0

main.py

from multiprocessing.pool import Pool
from mod1 import func1


def worker(val):
    global value 
    value = 121212
    
    print(val)

    func1()
    func2()
    ...
    ...


pool = Pool(3)
pool.map(worker, [{1:100},{2:200},{3:300}])
pool.close()
pool.join()

mod1.py

def func1():
    global value
    print(value)

I have a main module wherein I have a worker function which calls multiple functions in a sequential manner. I would like each of these functions to share a read only global variable say "value". Is there any way to achieve this ?

modo.dodo
  • 1
  • 1
  • Exactly what you need https://stackoverflow.com/questions/13034496/using-global-variables-between-files – alex_noname Jul 26 '20 at 08:42
  • And then there is also the [SyncManager](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.sharedctypes.multiprocessing.Manager). – Jeronimo Jul 26 '20 at 08:44
  • This would be an apt solution if multiple threads were not involved. In my case, I can have multiple threads and each thread should have its own global variable value (initialized once) which each function (func1, func2) should be able to read. – modo.dodo Jul 26 '20 at 19:40

0 Answers0