2

Because i cant run simply program with pool.apply_async and global variable, I need a help. I cant resolve a problem with shared memory. Short description a program flow:

How should this program works: Variable config.variable is a flag - default False. If something goes wrong in thread value of this flag should be set to Thru and value True should stop/Pause. In other words Idea is that fail inside a async proces should stop/pause program.

I tired do something with multiprocessing Value and Manager but with no result. I do not know it is my fail or it will never work. I am too weak for solving this problem. Not enough skills. Selv-learning is hard. I readed similar thread for example this Python share values or Python multiprocessing Pool.apply_async with shared variables (Value) but there is about passing argument into a thread. Most of others examples use Value or Manager but with Process and this solution work for me.

Question are: Can I use Value or Manager for pool.apply_async? How to do it for changing a global variable. What kinde of typecodes should I use for boolean True and False

I read this : Sharing state between processes

Please help me and show how should i write it. I attache simply code. Can somebody edit it and add missing lines? I can not change apply_async to process.

File config.py

variable1 = False

File main.py

import config
from multiprocessing import Pool
import time
import sys

def func2():
    try:
        config.variable1 = True
        print('Global Variable in thread {}'.format(config.variable1))
    except Exception as e:
        print(e)

if __name__ == '__main__':
    while 1:
        time.sleep(1)
        try:
            pool = Pool(4)
            pool.apply_async(func2)
            pool.close()
            pool.join()
        except Exception as e:
            print(e)
        # print(config.variable1)
        print('Global Variable in main loop {}'.format(config.variable1))


        if config.variable1 is True:
            sys.exit(0)

How to use Value or Manager in this case? Can somebody add few line?

Tank you for help and explanation.

martineau
  • 119,623
  • 25
  • 170
  • 301
luki
  • 197
  • 11
  • _Most of others examples use Value or Manager but with Process and this solution work for me._ I'm confused, what does or doesn't work? Also, I don't see the point of `except Exception as e:`, and I believe it is discouraged. I would suggest using a context manager for that pool, and writing `while True:` instead of `while 1:`. In fact, what is the purpose of that loop in the first place? – AMC Dec 30 '19 at 01:43
  • Do not work changing a global variable with pool.apply_asynch.While 1 is only for test reason. I wrote this because I waited for changing a value config.variable1.I tried use with Menager as menager: but I stuck. Value of my variable was not changed – luki Dec 30 '19 at 08:29
  • Martineau - from your answer was helpful only " I would suggest using a context manager for that pool" rest not on the topic. thx for help – luki Dec 30 '19 at 11:38
  • Okay, that makes a bit more sense. – AMC Dec 30 '19 at 17:20

1 Answers1

0

Solution for someone who will need it. I edited example from 16.6.1.4. Sharing state between processes¶

from multiprocessing import Pool, Manager
import config
from threading import Thread
import time

def f(d):

    if d['flag1'] is False:
        d['flag1'] = True
    else:
        d['flag1'] = False
    # l.reverse()

def stop():
    print('stop')
    while 1:
        if config.variable1 is True:
            break

if __name__ == '__main__':
    manager = Manager()
    print(config.variable1)

    d = manager.dict()

    thread1 = Thread(target = stop)
    thread1.start()

    while 1:
        d['flag1'] = config.variable1
        pool = Pool(4)
        p = pool.apply_async(f, args=(d,))
        pool.close()
        pool.join()

        config.variable1 =  d['flag1']

        print (d)
        print(config.variable1)
        if thread1.is_alive() is False:
            break
        time.sleep(3)
luki
  • 197
  • 11