3

I understand that from Python 3.2, the GIL works based on time of 5 miliseconds rather than 100 bytecodes before switching threads. However, if the operation needs a lock (needs to be atomic), then it waits for more than 5 milliseconds.

I want to know what the examples of operations that are atomic with respect to the GIL (e.g. assignment), non-atomic (e.g. increment) or release the GIL (e.g. reading). This will help programmer to know when to manage locks manually.

Example: Which category does the FOR/WHILE loops fall into?

So, based on the above list, the programmer has to implement his own locks to ensure that - for example - a non-atomic operation (increment) does not mess up the global variable value in a multi threaded program, because GIL only locks the atomic operation.

variable
  • 8,262
  • 9
  • 95
  • 215
  • *Every* Python operation needs locking in CPython/PyPy. That's the entire point of the GIL. Are you asking which ones are atomic with respect to the GIL (e.g. assignment), non-atomic (e.g. increment) or release the GIL (e.g. reading)? – MisterMiyagi Nov 04 '19 at 07:09
  • Yes, I will update the question thanks – variable Nov 04 '19 at 07:12

0 Answers0