在Python中将互斥量与并发期货一起使用

问题描述 投票:0回答:1

我有一些代码使用并发期货连接到许多远程主机以运行某些命令。

例如:

def set_host_to(host, value):
  connection = connect_to(host)
  info = do_something_with(connection)
  do_some_action(connection, value)

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
  for host, value in things_to_do:
    executor.submit(set_host_to, host, value)

我现在已经要求其中的某些期货不能同时运行,但是只有在获得上述info的情况下才能确定哪些期货。

这里正确的解决方案是某种互斥吗?假设info(或其中的一部分)是一个字符串,并且具有相同字符串的2个期货不应同时运行。我该如何编码?

我有一些代码使用并发期货连接到许多远程主机以运行某些命令。例如:def set_host_to(host,value):connection = connect_to(host)info = ...

python mutex concurrent.futures
1个回答
0
投票

这可以通过使用以下代码的字典来完成。

© www.soinside.com 2019 - 2024. All rights reserved.