简体   繁体   中英

Does GIL make the Python Interpreter single threaded or your python code single threaded

So I have been reading about the GIL but I haven't been able to make a certain subtle distinction. Is the Python interpreter (C process) single threaded itself when it is holding the GIL or can the interpreter be multithreaded (utilizing multiple cores, not the Python threading notion) but can only interpret one user level python thread at a time.

I am thinking about this because I am wondering if a C extension written in Cython did not release the GIL but utilized multiple threads would those threads be able to run on different cores? It seems if the user python program was single threaded and called on the Cython code for some long running operation the Cython code could be run on multiple cores without releasing the GIL (assuming no python interaction). Would it only be important for the Cython code to release the GIL in case the Python code calling on it was multithreaded so that other Python threads could be executed in the meantime by the interpreter while the Cython operation is being run on other cores?

The Global Interpreter Lock is just a lock. It's a lock that must be held to interact with almost every part of the CPython implementation machinery, including the C API, but it cannot do anything a lock cannot do.

The GIL will not automatically prevent parallel execution of threads that don't acquire the GIL. That means C extensions can run multiple threads in parallel, as long as those threads don't try to hold the GIL and don't do anything that needs the GIL.

On the other hand, the GIL will not automatically enable parallel execution of threads that don't need the GIL, if those threads try to hold the GIL anyway. In particular, if you want to write code in Cython that runs without the GIL, you have to release the GIL explicitly :

with nogil:
    ...

You can also mark a function as safe to call without the GIL by putting nogil in the function header:

cdef void my_gil_free_func(int spam) nogil:
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM