Python streams and sockets

I have a question, "I just want to understand." firstly i am using python 2.6.5 on ubuntu.

So, threads in python (via the threads module) are just β€œthreads”, and simply tells GIL to run blocks of code from each β€œthread” for a certain period of time, and so on .. and there aren really really topics here.

So the question is: if I have a blocking socket in one thread, and now I send data and block the thread in 5 seconds. I was expecting to block the entire program, because this is one C ( sock.send ) sock.send that blocks the thread. But I was surprised to see that the main thread continues to work. So the question is, how can the GIL continue and run the rest of the code after it reaches the lock command such as send? Is it necessary to use a real thread here?

Thanks.

+4
source share
3 answers

Python uses "real" threads, i.e. threads of the underlying platform. On Linux, it will use the pthread library (if you're interested, here is the implementation ).

A feature of Python threads is the GIL: a thread can only modify Python data structures if it contains this global lock. Thus, many Python operations cannot use multiple processor cores. A thread with a blocking socket will not hold the GIL, so it does not affect other threads.

GILs are often misunderstood, leading people to believe that threads are almost useless in Python. The only thing that prevents GIL is the simultaneous execution of "clean" Python code on multiple processor cores. If you use threads to respond to the GUI or to run different code while blocking I / O, GIL will not affect you. If you use threads to run code in some C extension, such as NumPy / SciPy, on multiple processor cores at the same time, GIL will not affect you either.

+11
source

The python GIL wiki page mentions that

Note that potentially blocking or lengthy operations, such as I / O , image processing, and NumPy number crunching, occur outside of the GIL.

+6
source

GIL (Global Interpreter Lock) is just a lock, it does not start anything by itself. Rather, the Python interpreter locks and releases the lock as needed. Typically, the lock is retained when Python code is run, but it is freed for calls to lower-level functions (for example, sock.send ). Since Python threads are real OS-level threads, threads will not run Python code in parallel, but if one thread calls the long-term C function, the GIL is freed and the other Python code thread can run until the first is completed.

+3
source

Source: https://habr.com/ru/post/1338706/


All Articles