Commit 64a6b48f authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook Github Bot

Save python import cost in folly/python get_fiber_manager

Summary:
Save python import cost in folly/python get_fiber_manager.

In my testing, using call_once here is safe even in multi-threaded and forked environment.
Though I wouldn't be surprised if a corner case is found in the future, as I couldn't find a definite answer about the safety of memoization Python C-API import calls.

Reviewed By: fried

Differential Revision: D9542909

fbshipit-source-id: c62a08ebf67bd4eb82f1bf4b7657c4a3a6daf4db
parent 4d234b99
......@@ -115,11 +115,13 @@ REMOVE_MATCHES_FROM_LISTS(files hfiles
list(REMOVE_ITEM files
${FOLLY_DIR}/experimental/JSONSchemaTester.cpp
${FOLLY_DIR}/experimental/io/HugePageUtil.cpp
${FOLLY_DIR}/python/fibers.cpp
${FOLLY_DIR}/python/GILAwareManualExecutor.cpp
)
list(REMOVE_ITEM hfiles
${FOLLY_DIR}/detail/SlowFingerprint.h
${FOLLY_DIR}/detail/FingerprintPolynomial.h
${FOLLY_DIR}/python/fibers.h
${FOLLY_DIR}/python/GILAwareManualExecutor.h
)
......
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/python/fibers.h>
#include <folly/fibers/CallOnce.h>
#include <folly/python/fiber_manager_api.h>
namespace folly {
namespace python {
folly::fibers::FiberManager* getFiberManager(
const folly::fibers::FiberManager::Options& opts) {
static folly::fibers::once_flag flag;
// Use call_once because Python performance is really poor,
// just to check if a module was already imported
folly::call_once(flag, [&]() {
// Use main context, because import can load arbitrary number of files,
// which is incompatible with fiber stack size restrictions
folly::fibers::runInMainContext([&]() {
import_folly__fiber_manager();
if (PyErr_Occurred() != nullptr) {
throw std::logic_error("Fail to import cython fiber_manager");
}
});
});
return get_fiber_manager(opts);
}
} // namespace python
} // namespace folly
......@@ -23,16 +23,12 @@
#include <Python.h>
#include <folly/Function.h>
#include <folly/fibers/FiberManagerInternal.h>
#include <folly/python/fiber_manager_api.h>
namespace folly {
namespace python {
inline folly::fibers::FiberManager* getFiberManager(
const folly::fibers::FiberManager::Options& opts = {}) {
import_folly__fiber_manager();
return get_fiber_manager(opts);
}
folly::fibers::FiberManager* getFiberManager(
const folly::fibers::FiberManager::Options& opts = {});
/**
* Helper function with similar callback/userData parameters as bridgeFuture.
......
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