Python Forum
Multithreading - shared mutable data - GIL
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multithreading - shared mutable data - GIL
#1
Hello.

Can the following code be considered threadsafe?
The important part for me is, if it could be considered threadsafe in future versions of Python without GIL.

In the past I learned the hard way that it is not a good idea to make assumptions on implementation details of underlying code esp. when it comes to multithreading.

Thanks a lot for your suggestions.

def worker(): while True: time.sleep(1) if not info['running']: # thread safe? break info = {} info['running'] = True th = threading.Thread(target = worker) th.start() time.sleep(5) info['running'] = False # thread safe? th.join()
Reply
#2
Hello,

the example is that shallow that it hardly makes sense... The thread is pretty useless here.

Generally speaking, relying on a global state of a variable - like here info should be prevented, as this can have unwanted side effects and makes it very hard to track the state of the program. When multiple threads have to work with the same resource concurrently, you may want to take a look at threading.Lock. An older, but IMHO still good introduction with examples can be found at https://pymotw.com/3/threading/ . Maybe this helps as a starting point.

Regards, noisefloor
Reply
#3
Hello noisefloor.

Thanks for taking the time to answer.

(Aug-01-2025, 05:37 PM)noisefloor Wrote: the example is that shallow that it hardly makes sense... The thread is pretty useless here.

This is of course true. I tried to remove any unnecessary code to make the example as short and easy as possible to include only the part which is relevant to the question.

So the essential question is if a 'bool' which is part of a 'dict' can be modified from one thread if it is read by another thread without any synchronization elements like mutex, etc. I know it will work currently as GIL will prevent any problems. But I want to be sure that code will not break if once GIL can or will be removed. For languages like C or C++ this is a much easier to answer.

Thanks.
Reply
#4
(Aug-01-2025, 06:39 PM)shortcut Wrote: I know it will work currently as GIL will prevent any problems.
I think this assumption is wrong. Yes, CPython with a GIL can only execute only one thread at a time - but this does _not_ imply automatic locking! As you have basically no control about context / thread switching in Python, it could well be that thread A reads a shared variable, then the thread is paused, Python switches to another thread B which reads, alters and writes the shared variable, then B is paused, Python return to A and continues to run this thread - with A having an outdated value for the shared variable. So basically your problem is _not_ a future problem, it is real for CPython today already.

Except this: C-based extension modules can have "true" multithreading even with CPython's GIL plus other Python implementations may not have a GIL anyway. The (in the meantime outdated?) Jython and IronPython implementations are GIL-free and it is my understanding that PyPy has no GIL either.

Regards, noisefloor
Reply
#5
(Aug-02-2025, 08:50 AM)noisefloor Wrote: As you have basically no control about context / thread switching in Python, it could well be that thread A reads a shared variable, then the thread is paused, Python switches to another thread B which reads, alters and writes the shared variable, then B is paused, Python return to A and continues to run this thread - with A having an outdated value for the shared variable.

This is clear. Only a single python opcode can be considered atomic, and there you can see that e.g. "x += 1" is not atomic cause it consists of multiple opcodes. But that is the reason I posted my example. The change of the shared value does not rely on a previous read value. So what you describe could not happen in my example.
Reply
#6
But then you don't have a problem anyway, without or with a GIL.

If this still doesn't answer your question, please post a better, closer to real-world code example than the pretty pointless example inn the original post.

Regards, noisefloor
Reply
#7
(Aug-04-2025, 12:25 PM)noisefloor Wrote: But then you don't have a problem anyway, without or with a GIL.

That was the answer to the initial question.
Thanks anyway.
Reply
#8
Just for the records.

I found an article which demonstrades what I tried to achieve (gracefully exit infinite loop of a thread from the main thread) in another way by using events.

https://www.instructables.com/Starting-a...-Events-i/
Reply
#9
Correct, forgot to mention that: threading.Event (https://docs.python.org/3/library/thread...nt-objects) is the simplest form for threads to communicate if "True/False" communication is sufficient. Just share the event object between threads.

Regards, noisefloor
Reply
#10
A recent, IMHO pretty nice blog post on Python, Threads, GIL and no GIL, the need for locks and shared data can be found at this link: https://blog.jetbrains.com/pycharm/2025/...eter-lock/

Regards, noisefloor
buran and shortcut like this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  multithreading xlwings error Equivocal 0 1,055 Jan-25-2025, 05:10 PM
Last Post: Equivocal
  using mutable in function defintion as optional paramter akbarza 8 3,115 Apr-27-2024, 09:59 PM
Last Post: snippsat
  mutable argument in function definition akbarza 1 1,611 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  mutable values to string items? fozz 15 6,869 Aug-30-2022, 07:20 PM
Last Post: deanhystad
  multithreading Hanyx 4 2,764 Jul-29-2022, 07:28 AM
Last Post: Larz60+
Question Problems with variables in multithreading Wombaz 2 2,539 Mar-08-2022, 03:32 PM
Last Post: Wombaz
  "'DataFrame' objects are mutable, thus they cannot be hashed" Mark17 1 9,237 Dec-25-2020, 02:31 AM
Last Post: tinh
  Multithreading question amadeok 0 2,646 Oct-17-2020, 12:54 PM
Last Post: amadeok
  Mutable Strings millpond 3 4,308 Aug-24-2020, 08:42 AM
Last Post: millpond
  How can i add multithreading in this example WoodyWoodpecker1 3 4,061 Aug-11-2020, 05:30 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.