fibers.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * This file serves as a helper for bridging folly fibers and python
  18. * asyncio.future.
  19. */
  20. #pragma once
  21. #include <Python.h>
  22. #include <folly/Function.h>
  23. #include <folly/fibers/FiberManagerInternal.h>
  24. namespace folly {
  25. namespace python {
  26. folly::fibers::FiberManager* getFiberManager(
  27. const folly::fibers::FiberManager::Options& opts = {});
  28. /**
  29. * Helper function with similar callback/userData parameters as bridgeFuture.
  30. * This can be convenient in code that calls both (notably our tests),
  31. * but most callsites should directly use getFiberManager().
  32. */
  33. template <typename T>
  34. void bridgeFibers(
  35. folly::Function<T()>&& function,
  36. folly::Function<void(folly::Try<T>&&, PyObject*)> callback,
  37. PyObject* userData) {
  38. auto* fiberManager = getFiberManager();
  39. // We are handing over a pointer to a python object to c++ and need
  40. // to make sure it isn't removed by python in that time.
  41. Py_INCREF(userData);
  42. auto guard = folly::makeGuard([=] { Py_DECREF(userData); });
  43. fiberManager->addTask([function = std::move(function),
  44. callback = std::move(callback),
  45. userData,
  46. guard = std::move(guard)]() mutable {
  47. // This will run from inside the gil, called by the asyncio add_reader
  48. auto res = folly::makeTryWith([&] { return function(); });
  49. callback(std::move(res), userData);
  50. // guard goes out of scope here, and its stored function is called
  51. });
  52. }
  53. } // namespace python
  54. } // namespace folly