简体   繁体   中英

Python multithreading and Global Interpreter Lock

I recently came across the GIL present in Python according to which only a single thread can execute at a time and multithreading can not utilize all the cores.

Now in one of my projects, I used multithreading and lots of locks and semaphores So here my question is can I achieve the same thing if I don't use locks and semaphores? ie if I remove the concurrency logics from my project.

Edit: What I want to know is, whether it's possible to attain same functionality if I remove concurrency logics, I know what is GIL and it prevents threads to use all the cores and only one thread is run at a time.

The Global Interpreter Lock ensures that only one thread is executing byte code at once. That execution could be interrupted at any time.

Consider this simple function which might be intended to atomically store related values to attributes on an instance x

def f(x, a, b):
    x.a, x.b = a, b

Here is its disassembly into bytecode

          0 LOAD_FAST                1 (a)
          3 LOAD_FAST                2 (b)
          6 ROT_TWO
          7 LOAD_FAST                0 (x)
         10 STORE_ATTR               0 (a)
         13 LOAD_FAST                0 (x)
         16 STORE_ATTR               1 (b)
         19 LOAD_CONST               0 (None)
         22 RETURN_VALUE

Suppose x is not protected by a mutex . Then any thread executing f(x, 1, 2) can easily be interrupted between storing a (at 10 ) and storing b (at 16 ). That interrupting thread will now see x in an inconsistent state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM