Does python queue use the GIL?

Asked by At

Does the python thread safe queue use the GIL, or does it use its own synchronization mechanism?

I want to know because I have a system as follows:

  • 2 threads read and write to a queue
  • 2 unrelated threads do their own thing, while synchronizing via the GIL

I want to know if the thread pairs are going to influence each other via the locking mechanisms.

1 Answers

Arthur Havlicek On

Based on source code of queue we can deduce python's safe Queues are implemented using python threading locks, which will block only the thread that waits for the queue object.

While they do not use explicitly the GIL, it is important to be aware that execution in blocked code will trigger the GIL as well nonetheless, as any code accessing variables trigger the GIL. If necessary you may document yourself on the gil browsing this question