How to use global variable in python, in a threadsafe way
Asked Answered
C

5

10

I want to use a global variable, Init it once. having a thread safe access.

Can someone share an example please?

Crater answered 20/2, 2013 at 11:57 Comment(2)
Googled python variable scope. First link : saltycrane.com/blog/2008/01/python-variable-scope-notesMyiasis
What do you mean by thread safe? blogs.msdn.com/b/ericlippert/archive/2009/10/19/…Caramel
M
7

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.

Mccutchen answered 20/2, 2013 at 12:6 Comment(0)
A
1

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.

Adelbert answered 20/2, 2013 at 12:25 Comment(0)
C
1

The threading library is what you want:

import threading
mydata = threading.local()
mydata.x = 1
Cacus answered 15/11, 2020 at 10:33 Comment(0)
P
0

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.

Pember answered 20/2, 2013 at 12:45 Comment(0)
G
0

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.

  • If you just want to read the data, then there are no race conditions to consider because the data does not change. So you do not need to do anything "special"

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

How to use a Mutex / Lock

The general idea is this:

  • When you need to access some shared data, first aquire the mutex, and then release the mutex when you have finished accessing the data
  • The purpose of a mutex is to protect the data by providing mutually exclusive (hence the name) access to the data
  • Only one thread can aquire a mutex at any one time. If a second thread calls the .aquire() function while another thread already holds the mutex, this function call will block until the mutex becomes available
  • When the thread holding the mutex calls the .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
Gardant answered 29/6, 2024 at 8:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.