python 子线程返回值_Python线程返回值

您可以将(worker,data)的元组推送到队列以标识源。

另外,请注意,由于全局解释器锁定,Python线程处理不是很有用。我建议看一下多处理模块,它提供的接口与多线程非常相似,但实际上会随着工作人员的数量而扩展。在

编辑:

代码示例。在import multiprocessing as mp

# py 3 compatibility

try:

from future_builtins import range, map

except ImportError:

pass

data = [

# input data

# {split_config: ... }

]

def crypto(split_config, asax, names):

# your code here

pass

if __name__ == "__main__":

terminate = mp.Event()

input = mp.Queue()

output = mp.Queue()

def worker(id, terminate, input, output):

# use event here to graciously exit

# using Process.terminate would leave queues

# in undefined state

while not terminate.is_set():

try:

x = input.get(True, timeout=1000)

output.put((id, crypto(**x)))

except Queue.Empty:

pass

workers = [mp.Process(target=worker, args=(i, )) for i in range(0, mp.cpu_count())]

for worker in workers:

worker.start()

for x in data:

input.put(x)

# terminate workers

terminate.set()

# process results

# make sure that queues are emptied otherwise Process.join can deadlock

for worker in workers:

worker.join()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值