1

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.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • threads in general use the GIL. threads in python are NOT concurrent – Nullman May 12 '19 at 10:15
  • @Nullman Thanks, I realize they are not concurrent, but if 2 thread pairs have to only synchronize with each other, would they also affect the other pair? – Gulzar May 12 '19 at 10:17
  • as they all share the gil, yes they would, unless of course they are running code which releases the gil – Nullman May 12 '19 at 10:23

1 Answers1

3

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

Diane M
  • 1,503
  • 1
  • 12
  • 23