Fix xlog docs
[folly.git] / folly / python / executor.pyx
1 import asyncio
2 from folly cimport cFollyExecutor
3 from folly.executor cimport cNotificationQueueExecutor
4 from libcpp.memory cimport make_unique, unique_ptr
5 from cython.operator cimport dereference as deref
6
7 #asynico Loops to NotificationQueueExecutor
8 loop_to_q = {}
9
10
11 cdef class NotificationQueueExecutor:
12    def __cinit__(self):
13        self.cQ = make_unique[cNotificationQueueExecutor]();
14
15    def fileno(NotificationQueueExecutor self):
16        return deref(self.cQ).fileno()
17
18    def drive(NotificationQueueExecutor self):
19        deref(self.cQ).drive()
20
21    def __dealloc__(NotificationQueueExecutor self):
22        # We drive it one last time
23        deref(self.cQ).drive()
24
25
26 cdef cFollyExecutor* get_executor():
27    loop = asyncio.get_event_loop()
28    try:
29        Q = <NotificationQueueExecutor>(loop_to_q[loop])
30    except KeyError:
31        Q = NotificationQueueExecutor()
32        loop.add_reader(Q.fileno(), Q.drive)
33        loop_to_q[loop] = Q
34    return Q.cQ.get()