From 1ef6035aea75edf251df42c267723757ee02089a Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Thu, 17 Apr 2014 22:52:13 -0700 Subject: [PATCH] Add functionality for using event loops in different threads --- capnp/lib/capnp.pyx | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 24be4be..ef6c824 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -1363,28 +1363,28 @@ cdef class Timer: def getTimer(): return Timer()._init(helpers.getTimer(C_DEFAULT_EVENT_LOOP_GETTER().thisptr)) -# cpdef remove_event_loop(): -# 'Remove the global event loop' -# global C_DEFAULT_EVENT_LOOP -# C_DEFAULT_EVENT_LOOP._remove() -# C_DEFAULT_EVENT_LOOP = None +cpdef remove_event_loop(): + 'Remove the global event loop' + global C_DEFAULT_EVENT_LOOP + C_DEFAULT_EVENT_LOOP._remove() + C_DEFAULT_EVENT_LOOP = None -# cpdef create_event_loop(is_thread_local=True): -# '''Create a new global event loop. This will not remove the previous -# EventLoop for you, so make sure to do that first''' -# global C_DEFAULT_EVENT_LOOP -# global _C_DEFAULT_EVENT_LOOP_LOCAL -# if is_thread_local: -# if _C_DEFAULT_EVENT_LOOP_LOCAL is None: -# _C_DEFAULT_EVENT_LOOP_LOCAL = _threading.local() -# _C_DEFAULT_EVENT_LOOP_LOCAL.loop = _EventLoop() -# else: -# C_DEFAULT_EVENT_LOOP = _EventLoop() +cpdef create_event_loop(threaded=True): + '''Create a new global event loop. This will not remove the previous + EventLoop for you, so make sure to do that first''' + global C_DEFAULT_EVENT_LOOP + global _C_DEFAULT_EVENT_LOOP_LOCAL + if threaded: + if _C_DEFAULT_EVENT_LOOP_LOCAL is None: + _C_DEFAULT_EVENT_LOOP_LOCAL = _threading.local() + _C_DEFAULT_EVENT_LOOP_LOCAL.loop = _EventLoop() + else: + C_DEFAULT_EVENT_LOOP = _EventLoop() -# cpdef reset_event_loop(): -# global C_DEFAULT_EVENT_LOOP -# C_DEFAULT_EVENT_LOOP._remove() -# C_DEFAULT_EVENT_LOOP = _EventLoop() +cpdef reset_event_loop(): + global C_DEFAULT_EVENT_LOOP + C_DEFAULT_EVENT_LOOP._remove() + C_DEFAULT_EVENT_LOOP = _EventLoop() def wait_forever(): cdef _EventLoop loop = C_DEFAULT_EVENT_LOOP_GETTER()