Python多处理调用类函数



我正在尝试多处理大量需要调用类函数的对象。我无法将函数拉出类外,因此只需要找到一种方法来加快对该函数的调用。

提前感谢

import time
# - Bespoke Class that is standalone - #
class sub_class:
def __init__(self, item_id, assumptions = "BlahBlahBlah"):
self.item_id = item_id
self.assumptions = assumptions
self.test_count = 0
def some_func(self, adder=1):
self.test_count += adder
time.sleep(0.1)

# - initialise a list of classes - #
item_ids = ['A', 'B', 'C', 'D']
items = [sub_class(_id) for _id in item_ids]
print( sum(_item.test_count for _item in items) )
# Prints out 0
# - invoke a class level function - #
[_item.some_func(adder=2) for _item in items]
print( sum(_item.test_count for _item in items) )
# Prints out 8

这只需要对some_func((进行一个小的更改。

import time
from concurrent.futures import ProcessPoolExecutor, wait, ALL_COMPLETED

# - Bespoke Class that is standalone - #
class MyClass:
def __init__(self, item_id, assumptions="BlahBlahBlah"):
self.item_id = item_id
self.assumptions = assumptions
self.test_count = 0
def some_func(self, adder=1):
self.test_count += adder
print(f"{self.item_id} executing some_func()")
time.sleep(0.1)
# note: we must return ourselves
return self

# Don't omit this on Windows
if __name__ == '__main__':
# - initialise a list of classes - #
item_ids = ['A', 'B', 'C', 'D']
items = [MyClass(item_id) for item_id in item_ids]
print(sum(item.test_count for item in items))
# Prints out 0
# - invoke a class level function - #
with ProcessPoolExecutor(max_workers=4) as executor:
future_list = []
for item in items:
future_list.append(executor.submit(item.some_func, adder=2))
wait(future_list, return_when=ALL_COMPLETED)
# note: we must use the returned self to get the test count
print(sum(future.result().test_count for future in future_list))
# Prints out 8
# note: the original items haven't changed
print(sum(item.test_count for item in items))
# Prints out 0

"items"被复制到新的Process中,因此您必须使用未来的return((值。因此,可以在wait()之后的某个时间点使用items = [future.result() for future in future_list]替换原始项。

相关内容

  • 没有找到相关文章

最新更新