python进程间共享数据_使用多处理的Python进程之间共享数据的问题

以下是一个建议的解决方案,假设您希望:控制工人寿命的集中管理者/主要过程

工作进程独立完成某项工作,然后将结果报告给经理和其他进程

在我展示之前,我想说的是,一般情况下,除非您是CPU专用的,否则{}实际上并不适合,主要是因为增加了复杂性,而且您最好使用不同的高级异步框架。另外,你应该使用python3,它好得多!在

也就是说,^{},使用multiprocessing可以很容易地实现这一点。我已经在Python3中完成了这项工作,但我不认为任何东西在Python2中不应该“只工作”,但我还没有检查过。在from ctypes import c_bool

from multiprocessing import Manager, Process, Array, Value

from pprint import pprint

from time import sleep, time

class Agent(Process):

def __init__(self, name, shared_dictionary, delay=0.5):

"""My take on your Agent.

Key difference is that I've commonized the run-loop and used

a shared value to signal when to stop, to demonstrate it.

"""

super(Agent, self).__init__()

self.name = name

# This is going to be how we communicate between processes.

self.shared_dictionary = shared_dictionary

# Create a silo for us to use.

shared_dictionary[name] = []

self.should_stop = Value(c_bool, False)

# Primarily for testing purposes, and for simulating

# slower agents.

self.delay = delay

def get_next_results(self):

# In the real world I'd use abc.ABCMeta as the metaclass to do

# this properly.

raise RuntimeError('Subclasses must implement this')

def run(self):

ii = 0

while not self.should_stop.value:

ii += 1

# debugging / monitoring

print('%s %s run loop execution %d' % (

type(self).__name__, self.name, ii))

next_results = self.get_next_results()

# Add the results, along with a timestamp.

self.shared_dictionary[self.name] += [(time(), next_results)]

sleep(self.delay)

def stop(self):

self.should_stop.value = True

print('%s %s stopped' % (type(self).__name__, self.name))

class HVACAgent(Agent):

def get_next_results(self):

# This is where you do your work, but for the sake of

# the example just return a constant dictionary.

return {'temperature': 5, 'pressure': 7, 'humidity': 9}

class DumbReadingAgent(Agent):

"""A dumb agent to demonstrate workers reading other worker values."""

def get_next_results(self):

# get hvac 1 results:

hvac1_results = self.shared_dictionary.get('hvac 1')

if hvac1_results is None:

return None

return hvac1_results[-1][1]['temperature']

# Script starts.

results = {}

# The "with" ensures we terminate the manager at the end.

with Manager() as manager:

# the manager is a subprocess in its own right. We can ask

# it to manage a dictionary (or other python types) for us

# to be shared among the other children.

shared_info = manager.dict()

hvac_agent1 = HVACAgent('hvac 1', shared_info)

hvac_agent2 = HVACAgent('hvac 2', shared_info, delay=0.1)

dumb_agent = DumbReadingAgent('dumb hvac1 reader', shared_info)

agents = (hvac_agent1, hvac_agent2, dumb_agent)

list(map(lambda a: a.start(), agents))

sleep(1)

list(map(lambda a: a.stop(), agents))

list(map(lambda a: a.join(), agents))

# Not quite sure what happens to the shared dictionary after

# the manager dies, so for safety make a local copy.

results = dict(shared_info)

pprint(results)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值