【问题标题】:How to get a list object to append values when using futures in Python?在 Python 中使用期货时如何获取列表对象以附加值?
【发布时间】:2019-12-06 18:56:42
【问题描述】:
from concurrent import futures

class MyClass:
    def __init__(self):
        self.mylist = []

    def test(self, i):
        self.mylist.append(i)

myclass = MyClass()

print(myclass.mylist)

ilist = [1, 2, 3, 4]

for i in ilist:
    myclass.test(i)

print(myclass.mylist)

myclass.mylist = []
with futures.ProcessPoolExecutor() as pool:
    for null in pool.map(myclass.test, ilist):
        pass

print(myclass.mylist)

输出:

[]
[1, 2, 3, 4]
[]

为什么将def test 中的值附加到self.mylist 可以在常规循环中工作,但在使用期货时却不行?使用期货时如何允许追加功能?

【问题讨论】:

  • 因为您使用的是多个进程,其中不共享状态

标签: python concurrent.futures


【解决方案1】:

让我们稍微调整一下程序,以便池执行的函数返回列表并打印MyClass 对象的地址。

from concurrent import futures

class MyClass:
    def __init__(self):
        self.mylist = []

    def test(self, i):
        print(hex(id(self)), self.mylist, i)
        self.mylist.append(i)
        return self.mylist

if __name__ == "__main__":
    myclass = MyClass()
    ilist = [1, 2, 3, 4]
    myclass.mylist = []
    with futures.ProcessPoolExecutor() as pool:
        for null in pool.map(myclass.test, ilist):
            print(f'Output of process: {null}')    
    print(f'addr: {hex(id(myclass))} , {myclass.mylist}')

给出输出

Output of process: [1]
Output of process: [2]
Output of process: [3]
Output of process: [4]
0x1b88e358860 [] 1
0x20bffa28908 [] 3
0x259844b87f0 [] 2
0x1d7546d8898 [] 4
addr: 0x20e5ebc5400 , []

如您所见,每个进程都在处理 MyClass 对象的不同副本。

现在让我们将ProcessPoolExecutor 替换为ThreadPoolExecutor。 现在结果如下所示:

0x1a323eb5438 [] 1
0x1a323eb5438 [1] 2
0x1a323eb5438 [1, 2] 3
0x1a323eb5438 [1, 2, 3] 4
Output of process: [1, 2, 3, 4]
Output of process: [1, 2, 3, 4]
Output of process: [1, 2, 3, 4]
Output of process: [1, 2, 3, 4]
addr: 0x1a323eb5438 , [1, 2, 3, 4]

现在每个线程都在处理同一个对象。

简而言之,进程有自己的内存,不会在进程之间共享。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 2016-03-09
    • 2021-05-22
    • 1970-01-01
    • 2017-06-09
    • 1970-01-01
    相关资源
    最近更新 更多