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 import asyncio
from folly.executor cimport cAsyncioExecutor from folly.executor cimport cAsyncioExecutor
from libcpp.memory cimport make_unique, unique_ptr from libcpp.memory cimport make_unique, unique_ptr
from cython.operator cimport dereference as deref from cython.operator cimport dereference as deref
from weakref import WeakKeyDictionary from weakref import WeakKeyDictionary
#asynico Loops to AsyncioExecutor # asyncio Loops to AsyncioExecutor
loop_to_q = WeakKeyDictionary() loop_to_q = WeakKeyDictionary()
cdef class AsyncioExecutor: cdef class AsyncioExecutor:
def __cinit__(self): def __cinit__(self):
self.cQ = make_unique[cAsyncioExecutor](); self.cQ = make_unique[cAsyncioExecutor]()
def fileno(AsyncioExecutor self): def fileno(AsyncioExecutor self):
return deref(self.cQ).fileno() return deref(self.cQ).fileno()
def drive(AsyncioExecutor self): def drive(AsyncioExecutor self):
deref(self.cQ).drive() deref(self.cQ).drive()
def __dealloc__(AsyncioExecutor self): def __dealloc__(AsyncioExecutor self):
# We drive it one last time # We drive it one last time
deref(self.cQ).drive() deref(self.cQ).drive()
cdef cAsyncioExecutor* get_executor(): cdef cAsyncioExecutor* get_executor():
loop = asyncio.get_event_loop() try:
try: loop = asyncio.get_event_loop()
Q = <AsyncioExecutor>(loop_to_q[loop]) except RuntimeError:
except KeyError: return NULL
Q = AsyncioExecutor() try:
loop.add_reader(Q.fileno(), Q.drive) Q = <AsyncioExecutor>(loop_to_q[loop])
loop_to_q[loop] = Q except KeyError:
return Q.cQ.get() 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