python multiprocessing 全局变量_Python - 如何将全局变量传递给multiprocessing.Process?...

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()

解决方案

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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值