Python - How to pass global variable to multiprocessing.Process?

社会主义新天地 提交于 2019-12-12 06:09:21

问题


I need to terminate some processes after a while, so I've used sleeping another process for the waiting. But the new process doesn't have access to global variables from the main process I guess. How could I solve it please?

Code:

import os
from subprocess import Popen, PIPE
import time
import multiprocessing


log_file = open('stdout.log', 'a')
log_file.flush()

err_file = open('stderr.log', 'a')
err_file.flush()

processes = []


def processing():
    print "processing"
    global processes
    global log_file
    global err_file

    for i in range(0, 5):
        p = Popen(['java', '-jar', 'C:\\Users\\two\\Documents\\test.jar'], stdout=log_file, stderr=err_file) # something long running
        processes.append(p)
    print len(processes)    # returns 5

def waiting_service():
    name = multiprocessing.current_process().name
    print name, 'Starting'
    global processes
    print len(processes)    # returns 0

    time.sleep(2)

    for i in range(0, 5):
        processes[i].terminate()
    print name, 'Exiting'

if __name__ == '__main__':
    processing()

    service = multiprocessing.Process(name='waiting_service', target=waiting_service)

    service.start()

回答1:


You should be using synchronization primitives.

Possibly you want to set an Event that's triggered after a while by the main (parent) process.

You may also want to wait for the processes to actually complete and join them (like you would a thread).

If you have many similar tasks, you can use a processing pool like multiprocessing.Pool.

Here is a small example of how it's done:

import multiprocessing
import time

kill_event = multiprocessing.Event()

def work(_id):
    while not kill_event.is_set():
        print "%d is doing stuff" % _id
        time.sleep(1)
    print "%d quit" % _id

def spawn_processes():
    processes = []

    # spawn 10 processes
    for i in xrange(10):
        # spawn process
        process = multiprocessing.Process(target=work, args=(i,))
        processes.append(process)
        process.start()
        time.sleep(1)

    # kill all processes by setting the kill event
    kill_event.set()

    # wait for all processes to complete
    for process in processes:
        process.join()

    print "done!"
spawn_processes()



回答2:


The whole problem was in Windows' Python. Python for Windows is blocking global variables to be seen in functions. I've switched to linux and my script works OK.

Special thanks to @rchang for his comment:

When I tested it, in both cases the print statement came up with 5. Perhaps we have a version mismatch in some way? I tested it with Python 2.7.6 on Linux kernel 3.13.0 (Mint distribution).



来源:https://stackoverflow.com/questions/28086030/python-how-to-pass-global-variable-to-multiprocessing-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!