Jim*_*hez 5 multithreading python-3.x
我有两个清单:
a = [1, 2, 3, 4]
b = [9, 8, 7, 6]
Run Code Online (Sandbox Code Playgroud)
我希望将这两个列表的每个组合作为参数传递给我正在执行多线程处理的函数:
def test(hello, world):
return hello + world
with ThreadPoolExecutor(max_workers=10) as executor:
future_to_stuff = { executor.submit(self._http_check_port, hello, world): ### }
for future in as_completed(future_to_port):
...
Run Code Online (Sandbox Code Playgroud)
我试图找出如何“解压缩”我的两个列表,以便在值的每个组合a,并b作为PARAMS给函数发送。
dka*_*ato 10
我通常使用以下列表理解。
future_to_stuff = [executor.submit(test, hello, world)
for hello, world in zip(a, b)]
Run Code Online (Sandbox Code Playgroud)
这是修改后的代码。
from concurrent.futures import ThreadPoolExecutor, as_completed
def test(hello, world):
return hello + world
def main(a, b):
with ThreadPoolExecutor(max_workers=10) as executor:
future_to_stuff = [executor.submit(test, hello, world)
for hello, world in zip(a, b)]
for future in as_completed(future_to_stuff):
print(future.result())
if __name__ == '__main__':
a = [1, 2, 3, 4]
b = [9, 8, 7, 6]
main(a, b)
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用.mapmethod 而不是.submit.
from concurrent.futures import ThreadPoolExecutor, as_completed
def test(hello, world):
return hello + world
def main(a, b):
with ThreadPoolExecutor(max_workers=10) as executor:
results = executor.map(test, a, b)
for result in results:
print(result)
if __name__ == '__main__':
a = [1, 2, 3, 4]
b = [9, 8, 7, 6]
main(a, b)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6886 次 |
| 最近记录: |