You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've want to extend an application in order let the customer create it's own extensions through Python. I choose to use PyBind11 for this.
The code is working as long the python interpreter is called from the same thread, thus creating a bottleneck really impacting the performance of the application. So i'm actually trying to reduce this penalty by enqueing the tasks in multiple threads on python side.
here is the following illustration of the architecture i want to achieve :
This seems to work fine when the python function called through pybind11 doing simple stuff (allocating np.array, print strings, ...) , but not anymore after any calls like a REST call (using requests module or httpx).
Using the parallel stack debug view from my favourite IDE, i can observe that the process is stucked into the method take_gil from CPython.
As far i understand, my attempt is different from everything i could have read on the internet around using the py::gil_scoped_release because i'm not calling my C++ from Python, and pybind11 needs to get the GIL in order to wrap informations to python.
Before diving into code details, my questions are the following :
Can this work ?
If no : If no what are the options ?
How to find what is locking the GIL from inside python ?
...
Here is a minimal example of the freezing code, the python code, and the C++ that requires only boost and pybind as dependecies. Note that the function call_callback is defined by C++ code. When i uncomment call_callback and comment the other lines into the worker_thread function the GIL doesn't behave as a deadlock
AsyncCustomTasks.py
import configure_venv_modules
import random
import threading
#import httpx
import numpy as np
import requests
def enqueueTask(message: str, cmdId: int) -> int:
def worker_thread():
print(f"---------------------- Python : Worker thread for {message} and cmdId {cmdId}")
#url = 'http://dskgre1073l:9001/v1/models/resnet50/metadata'
url = 'https://www.example.org/'
print(f"---------------------- Python : ready to call httpx on {url}")
#response = httpx.get(url)
mat = np.zeros((512,512), np.float32)
print(f"---------------------- Python : Successfully created np.array")
response = requests.get(url)
print(f"---------------------- {response.text}") # This line never appears on std::out
# print(f"---------------------- Python : try to call httpx.get")
# response = httpx.get(url)
#response = httpx.post(url, json=data)
#print(f"---------------------- {response}")
call_callback(cmdId)
print(f"---------------------- Let's thread the processing of {message}")
wt = threading.Thread(target=worker_thread)
wt.start()
worker_thread.wt = wt
print(f"---------------------- {message} threaded")
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello everyone,
I've want to extend an application in order let the customer create it's own extensions through Python. I choose to use PyBind11 for this.
The code is working as long the python interpreter is called from the same thread, thus creating a bottleneck really impacting the performance of the application. So i'm actually trying to reduce this penalty by enqueing the tasks in multiple threads on python side.
here is the following illustration of the architecture i want to achieve :

This seems to work fine when the python function called through pybind11 doing simple stuff (allocating np.array, print strings, ...) , but not anymore after any calls like a REST call (using requests module or httpx).
Using the parallel stack debug view from my favourite IDE, i can observe that the process is stucked into the method take_gil from CPython.
As far i understand, my attempt is different from everything i could have read on the internet around using the py::gil_scoped_release because i'm not calling my C++ from Python, and pybind11 needs to get the GIL in order to wrap informations to python.
Before diving into code details, my questions are the following :
Here is a minimal example of the freezing code, the python code, and the C++ that requires only boost and pybind as dependecies. Note that the function call_callback is defined by C++ code. When i uncomment call_callback and comment the other lines into the worker_thread function the GIL doesn't behave as a deadlock
AsyncCustomTasks.py
and here is the standalone cpp file :
Thank you for giving me any clue, tips or advices
Beta Was this translation helpful? Give feedback.
All reactions