fibers.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #include <folly/python/fibers.h>
  17. #include <folly/fibers/CallOnce.h>
  18. #include <folly/python/fiber_manager_api.h>
  19. namespace folly {
  20. namespace python {
  21. folly::fibers::FiberManager* getFiberManager(
  22. const folly::fibers::FiberManager::Options& opts) {
  23. static folly::fibers::once_flag flag;
  24. // Use call_once because Python performance is really poor,
  25. // just to check if a module was already imported
  26. folly::call_once(flag, [&]() {
  27. // Use main context, because import can load arbitrary number of files,
  28. // which is incompatible with fiber stack size restrictions
  29. folly::fibers::runInMainContext([&]() {
  30. import_folly__fiber_manager();
  31. if (PyErr_Occurred() != nullptr) {
  32. throw std::logic_error("Fail to import cython fiber_manager");
  33. }
  34. });
  35. });
  36. return get_fiber_manager(opts);
  37. }
  38. } // namespace python
  39. } // namespace folly