Python transferring variables to function using concurrent.futures

Viewed 27

I have this test code using "concurrent.futures.ProcessPoolExecutor() as executor" and tried many combinations, but I can't get it it to work. I am certainly missing some syntax.

What this should be doing is share the x variable across the two functions and foo2 prints out the modified one.

def foo(x):
    while True:
        time.sleep(1)
        print('foo')
        x.value += 1

def foo2(y):
    while True:
        time.sleep(1.5)
        print('foo2')
        print(y.value)

def main():
    x = Value('i')

    with concurrent.futures.ProcessPoolExecutor() as executor:    
        executor.submit(foo, x) 
        executor.submit(foo2, x)

if __name__ == '__main__':
    main()

I tried these combos, but all not working:

(foo(x), x) 
(foo2(x), x) 


(foo(x)) 
(foo2(x)) 

(foo(x), x) 
(foo2(x), x) 

(foo, x) 
(foo2, x) 

(foo, x) 
(foo2, x) 

when I try these both combinations I get at least some (unshared variable) output:

x += 1
print(x)

(foo, x,value) 
(foo2, x.value) 

Output:

foo
foo2
0
foo
foo
foo2
0

I then modified a different script and got this to work:

def foo(y, name=''):
    while True:
        time.sleep(1)
        y.value += 1
        # print(y.value, name)

def foo2(y, name=''):
    while True:
        time.sleep(0.5)
        print(y.value)
    
if __name__ == "__main__":

    y = Value('i')
    
    Process( target=foo, args=(y, 'foo') ).start()
    Process( target=foo2, args=(y, 'foo2') ).start()

Output:

0
1
1
2
2
3

Really wanting it to make it work using the "executor.submit" feature. I also don't understand why the "Process" feature strictly requires 'foo' and name='' (otherwise it work work either)?

I am sure there is more to learn here! Any pointers greatly appreciated! I suspect some syntax error.

0 Answers
Related