简体   繁体   中英

Is compiled multi-threaded Python affected by the GIL

As the GIL is a lock that surrounds the interpreter does it affect compiled Python? I'm wondering whether it is possible to get past the inherent multi-threading limitations of cpython by simply compiling my python before executing it.

Hopefully that makes sense and I'm not missing something obvious or misinterpreting how the GIL works/affects execution.

Thanks

As Daniel said in the comments, it depends on how you "compile" the code.

For example, running the code using Jython does indeed get around the limitations imposed by the GIL.

On the other hand, using something like py2exe makes no difference, since this effectively just packages CPython alongside your code.

Jython does not have a GIL.

IronPython does not have a GIL.

You can compile your python code with cython, and then whether it uses the GIL or not depends. If you convert all you python variables into cython types, you can run your code in a with nogil block and you will have no GIL because you are expressly releasing the GIL. If you are not running in a nogil block, you will be affected by cpython's GIL. More in the cython docs: http://docs.cython.org/src/userguide/external_C_code.html#acquiring-and-releasing-the-gil

For more on python and the GIL, read up here: http://www.jeffknupp.com/blog/2013/06/30/pythons-hardest-problem-revisited/

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