Folly Futures to Python Asyncio Futures Bridge
[folly.git] / folly / python / test / simplebridge.pyx
1 import asyncio
2 from folly.futures cimport bridgeFuture
3 from folly cimport cFollyFuture, cFollyTry
4 from libc.stdint cimport uint64_t
5 from cpython.ref cimport PyObject
6 from cython.operator cimport dereference as deref
7
8 cdef extern from "folly/python/test/simple.h" namespace "folly::python::test":
9     cdef cFollyFuture[uint64_t] future_getValueX5(uint64_t val)
10
11
12 def get_value_x5(int val):
13     loop = asyncio.get_event_loop()
14     fut = loop.create_future()
15     bridgeFuture[uint64_t](
16         future_getValueX5(val),
17         handle_uint64_t,
18         <PyObject *>fut
19     )
20     return fut
21
22
23 cdef void handle_uint64_t(cFollyTry[uint64_t]&& res, PyObject* userData):
24     future = <object> userData
25     if res.hasException():
26         try:
27             res.exception().throwException()
28         except Exception as ex:
29             future.set_exception(ex)
30     else:
31         future.set_result(res.value())