1

I am trying to pass a value from one function to another but it seems its not doing so. here is my code:

x = 0
y = 1

def funcTwo():
    global x,y
    
    while x < 3:
        x = x + 1
        print('this is x= ' + str(x))
        time.sleep(1)
        if x == 3:
            x = 0
            
def funcOne():
    global x,y
    while y < 4:
        y = x + y
        print('this is y= '+str(y))
        time.sleep(1)
        if y == 4:
            y = 1


    
if __name__ == "__main__":
    
                
    def runInParallel(*fns):
        proc = []
        for fn in fns:
            p = Process(target=fn)
            p.start()
            proc.append(p)
        for p in proc:
            p.join() 
    
 runInParallel(funcOne, funcTwo)

the variable x is not moving to the second function? is there a need to return the variable x or something ?

Jun
  • 49
  • 6
  • 1
    processes do not share memory space, global variables won't work. Use something like queues/pipes or Pools – Charchit Agarwal Aug 28 '22 at 11:03
  • "If you are running two separate processes, then they won't be sharing the same globals." according to [Globals variables and Python multiprocessing](https://stackoverflow.com/questions/11215554/globals-variables-and-python-multiprocessing/11215750#11215750), Perhaps you could make use of the [Shared Memory Module](https://docs.python.org/3/library/multiprocessing.shared_memory.html) in Python 3.8+. – DarrylG Aug 28 '22 at 11:04

0 Answers0