简体   繁体   English

python池映射和apply_async

[英]python pool map and apply_async

I'm trying to learn about map and apply_async in the following example (adapted from here ). 我试图在下面的示例中学习mapapply_async (从此处改编)。

from multiprocessing import Pool, Manager
from functools import partial
import time

def operate(number1=None, number2=None, op=None, timeout_list=None):
    if op == "+":
        return number1 + number2
    elif op == "-":
        time.sleep(1)
        return number1 - number2
    elif op == "/":
        time.sleep(6)
        return number1 / number2
    elif op == "*":
        time.sleep(10)
        return number1 * number2
    else:
        print("+, -, /, *")

timeout_list = Manager().list()
kwargs = {"number1":10, "number2":7, "timeout_list":timeout_list}

if __name__ == '__main__':
    ops = ["+", "+", "-", "*", "/", "+"]
    pool = Pool(processes=len(ops))
    result = pool.map(partial(operate, kwargs),ops)
    print(result.get(timeout=5))

I'm trying to do three things: 我正在尝试做三件事:

  1. pass op as a keyword to map after partially setting operate with number1 and number2 (but I'm currently getting an AttributeError: 'list' object has no attribute 'get' ) 通过对number1number2进行部分设置operate后,将op作为关键字进行map (但我目前正在得到AttributeError: 'list' object has no attribute 'get'
  2. get the operations * and / to timeout and return * and / (respectively) as results from timing out in timeout_list 将操作*/进行超时,并分别从timeout_list时返回*/
  3. how would I achieve the following with apply_async and what's the difference? 我如何通过apply_async实现以下apply_async ,有什么区别?

Question : ... pass op as a keyword to map 问题 :...将op作为关键字传递给地图

You have , put the following in def operate(... to see what you do: 您已将以下内容放入def operate(...以查看您的操作:

def operate(number1=None, number2=None, op=None, timeout_list=None):
    print('pid:{} operate({})'.format(os.getpid(), (number1, number2, op, timeout_list)))

>>>pid:4932 operate(({'number1': 10, 'number2': 7, 'timeout_list': <ListProxy object, typeid 'list' at 0xf6c0afac>}, 
                     '+', 
                     None, 
                     None)
                   )
  1. number1 == kwargs dict number1 == kwargs dict
  2. number2 == ops[x] number2 == ops[x]
  3. op == None , because you use only 2 Parameters op == None ,因为仅使用2个参数
  4. timeout_list == None , because you use only 2 Parameters timeout_list == None ,因为您仅使用2个参数

Question : ... partially setting operate with number1 and number2 问题 :...部分设置使用数字1和数字2进行操作

You haven't , do the following to see what partial do: 您还没有 ,请执行以下操作查看partial操作:

print(partial(operate, kwargs))

>>>functools.partial(<function operate at 0xf70ed194>, 
                      {'number1': 10, 'number2': 7, 'timeout_list': <ListProxy object, typeid 'list' at 0xf70f38ec>})

Question : ... getting an AttributeError: 'list' object has no attribute 'get') 问题 :...正在获取AttributeError:“ list”对象没有属性“ get”)
Read the AttributeError carefully! 仔细阅读AttributeError

.map(... returns a pure Python list not a result object ! .map(...返回纯Python list而不是结果对象

 print(result)
>>>[None, None, None, None, None, None]

Question : 2. get the operations * and / to timeout and return ... 问题 :2.获取操作*和/到超时并返回...

Unclear what you are asking? 不清楚你在问什么?

Question : 3. how would I achieve the following with apply_async ... 问题 :3.如何使用apply_async实现以下目标?

Before you get further get Step 1 and 2 running. 在进一步了解之前,请先运行步骤1和2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM