I want to use a global variable, Init it once. having a thread safe access.
Can someone share an example please?
I want to use a global variable, Init it once. having a thread safe access.
Can someone share an example please?
If you need read-only access and the value is initialized before threads are spawn, you don't need to worry about thread safety.
If that is not the case Python threading library is probably what you need, more precisely locks. A really good read on the subject - http://effbot.org/zone/thread-synchronization.htm with quite a lot of examples.
You do have a problem if you are using multiprocessing.Processes. In which case you should take a look at Managers and Queues in the multiprocessing module.
The threading library is what you want:
import threading
mydata = threading.local()
mydata.x = 1
If you initialize it once, and if you initialize it on when module is loaded (that means: before it can be accessed from other threads), you will have no problems with thread safety at all. No synchronization is needed.
But if you mean a more complex scenario, you have to explain it further to get a reasonable code example.
This is a really old question, but I happened to come across it.
It isn't clear from your question if you wanted read-write access to the data or just read access.
However, if you require the more general case of both read and write access to some data which is shared by multiple threads, you need to protect that data using a mutex. (Python calls them locks in the threading library.)
https://docs.python.org/3/library/threading.html#lock-objects
The general idea is this:
.aquire()
function while another thread already holds the mutex, this function call will block until the mutex becomes available.release()
function, other threads are then able to call .aquire()
A mutex is just a piece of data or variable just like any other. What it actually implements however is an object which provides a synchronization point between multiple threads. There are different possible implementations, some based on atomic CPU instructions, some based on Operating System level system calls.
The general idea, sketched out, is something like this:
lock.aquire() # other threads cannot aquire the lock now
shared_data = some value # read/write to protected shared data
lock.release() # other threads now can aquire the lock again
© 2022 - 2025 — McMap. All rights reserved.