proxygen
folly::python Namespace Reference

Namespaces

 test
 

Classes

class  AsyncioExecutor
 
class  GILAwareManualExecutor
 

Functions

folly::fibers::FiberManagergetFiberManager (const folly::fibers::FiberManager::Options &opts)
 
template<typename T >
void bridgeFibers (folly::Function< T()> &&function, folly::Function< void(folly::Try< T > &&, PyObject *)> callback, PyObject *userData)
 
folly::ExecutorgetExecutor ()
 
template<typename T >
void bridgeFuture (folly::Executor *executor, folly::Future< T > &&futureFrom, folly::Function< void(folly::Try< T > &&, PyObject *)> callback, PyObject *userData)
 
template<typename T >
void bridgeFuture (folly::Future< T > &&futureFrom, folly::Function< void(folly::Try< T > &&, PyObject *)> callback, PyObject *userData)
 

Function Documentation

template<typename T >
void folly::python::bridgeFibers ( folly::Function< T()> &&  function,
folly::Function< void(folly::Try< T > &&, PyObject *)>  callback,
PyObject *  userData 
)

Helper function with similar callback/userData parameters as bridgeFuture. This can be convenient in code that calls both (notably our tests), but most callsites should directly use getFiberManager().

Definition at line 39 of file fibers.h.

References getFiberManager(), folly::gen::guard(), folly::makeGuard(), folly::makeTryWith(), and folly::gen::move.

42  {
43  auto* fiberManager = getFiberManager();
44  // We are handing over a pointer to a python object to c++ and need
45  // to make sure it isn't removed by python in that time.
46  Py_INCREF(userData);
47  auto guard = folly::makeGuard([=] { Py_DECREF(userData); });
48  fiberManager->addTask([function = std::move(function),
49  callback = std::move(callback),
50  userData,
51  guard = std::move(guard)]() mutable {
52  // This will run from inside the gil, called by the asyncio add_reader
53  auto res = folly::makeTryWith([&] { return function(); });
54  callback(std::move(res), userData);
55  // guard goes out of scope here, and its stored function is called
56  });
57 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
Definition: ScopeGuard.h:184
std::enable_if< !std::is_same< invoke_result_t< F >, void >::value, Try< invoke_result_t< F > > >::type makeTryWith(F &&f)
Definition: Try-inl.h:223
FiberManager & getFiberManager(EventBase &evb, const FiberManager::Options &opts)
template<typename T >
void folly::python::bridgeFuture ( folly::Executor executor,
folly::Future< T > &&  futureFrom,
folly::Function< void(folly::Try< T > &&, PyObject *)>  callback,
PyObject *  userData 
)

Definition at line 38 of file futures.h.

References folly::gen::guard(), folly::makeGuard(), and folly::gen::move.

Referenced by bridgeFuture().

42  {
43  // We are handing over a pointer to a python object to c++ and need
44  // to make sure it isn't removed by python in that time.
45  Py_INCREF(userData);
46  auto guard = folly::makeGuard([=] { Py_DECREF(userData); });
47  // Handle the lambdas for cython
48  // run callback from our Q
49  futureFrom.via(executor).then(
50  [callback = std::move(callback), userData, guard = std::move(guard)](
51  folly::Try<T>&& res) mutable {
52  // This will run from inside the gil, called by the asyncio add_reader
53  callback(std::move(res), userData);
54  // guard goes out of scope here, and its stored function is called
55  });
56 }
Future< T > via(Executor *executor, int8_t priority=Executor::MID_PRI)&&
Definition: Future-inl.h:1024
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
Definition: Try.h:51
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
Definition: ScopeGuard.h:184
template<typename T >
void folly::python::bridgeFuture ( folly::Future< T > &&  futureFrom,
folly::Function< void(folly::Try< T > &&, PyObject *)>  callback,
PyObject *  userData 
)

Definition at line 59 of file futures.h.

References bridgeFuture(), getExecutor(), and folly::gen::move.

62  {
64  getExecutor(), std::move(futureFrom), std::move(callback), userData);
65 }
folly::Executor * getExecutor()
Definition: futures.h:32
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void bridgeFuture(folly::Future< T > &&futureFrom, folly::Function< void(folly::Try< T > &&, PyObject *)> callback, PyObject *userData)
Definition: futures.h:59
folly::Executor* folly::python::getExecutor ( )
inline

Definition at line 32 of file futures.h.

Referenced by bridgeFuture(), folly::Future< folly::folly::Unit >::then(), and folly::Future< folly::folly::Unit >::thenMultiWithExecutor().

32  {
33  import_folly__executor();
34  return get_executor();
35 }
folly::fibers::FiberManager * folly::python::getFiberManager ( const folly::fibers::FiberManager::Options opts)

Definition at line 24 of file fibers.cpp.

References folly::call_once(), flag, and folly::fibers::runInMainContext().

Referenced by bridgeFibers().

25  {
27  // Use call_once because Python performance is really poor,
28  // just to check if a module was already imported
29  folly::call_once(flag, [&]() {
30  // Use main context, because import can load arbitrary number of files,
31  // which is incompatible with fiber stack size restrictions
33  import_folly__fiber_manager();
34  if (PyErr_Occurred() != nullptr) {
35  throw std::logic_error("Fail to import cython fiber_manager");
36  }
37  });
38  });
39  return get_fiber_manager(opts);
40 }
static once_flag flag
Definition: Random.cpp:75
FOLLY_ALWAYS_INLINE void call_once(basic_once_flag< Mutex, Atom > &flag, F &&f, Args &&...args)
Definition: CallOnce.h:56
invoke_result_t< F > runInMainContext(F &&func)