Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is dlib Python API releasing GIL when calls C++ functions? #1403

Closed
meownoid opened this issue Jul 5, 2018 · 12 comments
Closed

Is dlib Python API releasing GIL when calls C++ functions? #1403

meownoid opened this issue Jul 5, 2018 · 12 comments
Labels

Comments

@meownoid
Copy link

meownoid commented Jul 5, 2018

I'm wondering if I can use dlib with python multithreading.

@supervacuus
Copy link
Contributor

The default behavior in pybind11 is to hold the GIL when calling C++ from Python:

http://pybind11.readthedocs.io/en/stable/advanced/misc.html#global-interpreter-lock-gil

There is currently no GIL-release/acquire in the python wrapper.

@davisking
Copy link
Owner

We could add statements to unlock the GIL, however, it's not clear to me what a python user then does with that. Any python code you write is going to reengage the GIL. So it doesn't seem like you can do very much. Or is there some important use case that would be enabled by this?

@meownoid
Copy link
Author

meownoid commented Jul 8, 2018

Consider a web service for face recognition. Each client gets it's own thread, something like this:

@run_async
def generate_response(request):
    photo = load_photo(request)  # IO, releases GIL
    photo = process_photo_with_pillow(photo)  # releases GIL
    person = recognize_face_with_dlib(photo)  # oops
    upload_result(photo, person)  # IO, releases GIL

So, if generate_response is running in separate thread using threading module, GIL is acquired just for little amount of time between the IO and C-calls. Except for the dlib which enforces all other user's threads to wait. This is a real example of project on that I'm currently working on and all other libraries (numpy, Pillow, requests) releases GIL, but not dlib. And I have to move all dlib calls to another process and think about communication and synchronization between processes...

@davisking
Copy link
Owner

davisking commented Jul 8, 2018 via email

@meownoid
Copy link
Author

meownoid commented Jul 8, 2018

How are you going to have multiple threads running it?

When GIL is released and dlib computation is running in several threads, they all running simultaneously on several cores.

When GIL is acquired only one of this threads can be running at this time and all others are waiting for it.

thread_1 = Thread(target=dlib_computation)
thread_2 = Thread(target=dlib_computation)

thread_1.start()
thread_2.start()
GIL acquired:

<=== thread 1 ===>                    <some Python code> <=== thread 1 ===>
                   <=== thread 2 ===>                                       <=== thread 2 ===>
GIL released:

<=== thread 1 ===> <some Python code> <=== thread 1 ===>
<=== thread 2 ===>                    <=== thread 2 ===>

So, I'm talking not about paralleling one dlib computation, but about running several dlib computations in parallel, which requires either threading + GIL releasing or multiprocessing.

@davisking
Copy link
Owner

Yes, I understand. But if the underlying thing in dlib you are calling is already using all the CPU cores, which it should be, then what you are talking about isn't going to do you any good. That's my point.

But in any case, if someone wants to implement this and submit a PR that's fine with me.

@meownoid
Copy link
Author

meownoid commented Jul 9, 2018

But if the underlying thing in dlib you are calling is already using all the CPU cores

Does it? Sorry, I couldn't find any information about this in documentation. In my observations it uses only one core. Is there any parameters for internal multi-threading in Python API?

@davisking
Copy link
Owner

You should be linking to a multi-threaded BLAS library. Then it will use all the cores.

@dlib-issue-bot
Copy link
Collaborator

Warning: this issue has been inactive for 57 days and will be automatically closed on 2018-09-07 if there is no further activity.

If you are waiting for a response but haven't received one it's likely your question is somehow inappropriate. E.g. you didn't follow the issue submission instructions, or your question is easily answerable by reading the FAQ, dlib's documentation, or a Google search.

@dlib-issue-bot
Copy link
Collaborator

Notice: this issue has been closed because it has been inactive for 60 days. You may reopen this issue if it has been closed in error.

@zhangchn
Copy link

zhangchn commented Jun 21, 2019

A single line of

 py::gil_scoped_release release;

at the beginning of each code block may help.

At least with this it keeps the main thread going and updating video captures in time.

@davisking
Copy link
Owner

Yes, that would do it. Someone who is using this and wants the feature should do this, test it and make sure it really hits their use case, and submit a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants