On Fri, 15 Nov 2019 14:21:53 -0800 Nathaniel Smith <n...@pobox.com> wrote: > As you know, I'm skeptical that PEP 554 will produce benefits that are > worth the effort, but let's assume for the moment that it is, and > we're all 100% committed to moving all globals into the threadstate. > Even given that, the motivation for this change seems a bit unclear to > me. > > I guess the possible goals are: > > - Get rid of the "ambient" threadstate entirely > - Make accessing the threadstate faster > > For the first goal, I don't think this is possible, or desirable. > Obviously if we remove the GIL somehow then at a minimum we'll need to > make the global threadstate a thread-local. But I think we'll always > have to keep it around as a thread-local, at least, because there are > situations where you simply cannot pass in the threadstate as an > argument. One example comes up when doing FFI: there are C libraries > that take callbacks, and will run them later in some arbitrary thread. > When wrapping these in Python, we need a way to bundle up a Python > function into a C function that can be called from any thread. So, > ctypes and cffi and cython all have ways to do this bundling, and they > all start with some delicate dance to figure out whether or not the > current thread holds the GIL, acquiring the GIL if not, then checking > whether or not this thread has a Python threadstate assigned, creating > it if not, etc. This is completely dependent on having the threadstate > available in ambient context. If threadstates were always passed as > arguments, then it would become impossible to wrap these C libraries.
Most well-designed C libraries let you pass an additional "void*" parameter for user callbacks to be called with. A couple of them don't, unfortunately (OpenSSL perhaps? I don't remember). Regards Antoine. _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/PUUP6NN6U6L7XTVYJQGPUW6LT5P6Y253/ Code of Conduct: http://python.org/psf/codeofconduct/