Commit 9a34add5 authored by Jason Fried's avatar Jason Fried Committed by Facebook Github Bot

Don't raise ignored exception return nullptr instead

Summary:
Sometimes get_executor gets called from threads where there is no asyncio evenloop.
In those cases return NULL.

The caller is responsible for this situation.

Reviewed By: yfeldblum

Differential Revision: D8492709

fbshipit-source-id: 325689ecd4541b9ba097cfe1cb4d6d61ad81cb43
parent 64eca5b8
import asyncio
from folly.executor cimport cAsyncioExecutor
from libcpp.memory cimport make_unique, unique_ptr
from cython.operator cimport dereference as deref
from weakref import WeakKeyDictionary
#asynico Loops to AsyncioExecutor
# asyncio Loops to AsyncioExecutor
loop_to_q = WeakKeyDictionary()
cdef class AsyncioExecutor:
def __cinit__(self):
self.cQ = make_unique[cAsyncioExecutor]();
def __cinit__(self):
self.cQ = make_unique[cAsyncioExecutor]()
def fileno(AsyncioExecutor self):
return deref(self.cQ).fileno()
def fileno(AsyncioExecutor self):
return deref(self.cQ).fileno()
def drive(AsyncioExecutor self):
deref(self.cQ).drive()
def drive(AsyncioExecutor self):
deref(self.cQ).drive()
def __dealloc__(AsyncioExecutor self):
# We drive it one last time
deref(self.cQ).drive()
def __dealloc__(AsyncioExecutor self):
# We drive it one last time
deref(self.cQ).drive()
cdef cAsyncioExecutor* get_executor():
loop = asyncio.get_event_loop()
try:
Q = <AsyncioExecutor>(loop_to_q[loop])
except KeyError:
Q = AsyncioExecutor()
loop.add_reader(Q.fileno(), Q.drive)
loop_to_q[loop] = Q
return Q.cQ.get()
try:
loop = asyncio.get_event_loop()
except RuntimeError:
return NULL
try:
Q = <AsyncioExecutor>(loop_to_q[loop])
except KeyError:
Q = AsyncioExecutor()
loop.add_reader(Q.fileno(), Q.drive)
loop_to_q[loop] = Q
return Q.cQ.get()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment