在 Python 中使用具有并发期货的互斥锁 [英] Using mutexes with concurrent futures in Python

查看:71
本文介绍了在 Python 中使用具有并发期货的互斥锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如:

def set_host_to(host, value):连接 = 连接到(主机)信息 = do_something_with(连接)do_some_action(连接,值)以 concurrent.futures.ThreadPoolExecutor(max_workers=5) 作为执行者:对于主机,things_to_do 中的值:executor.submit(set_host_to,主机,值)

我现在要求其中一些期货不能同时运行,但在获得上述 info 之前无法确定哪些期货.

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

解决方案

这可以通过锁字典来完成,使用下面的代码.

locks = defaultdict(threading.Lock)main_lock = threading.Lock()使用 main_lock:锁=锁[信息]带锁:do_some_action(连接,值)

我不确定主锁是否必要,但这似乎有效.

I have some code that uses concurrent futures to connect to lots of remote hosts to run some commands.

For example:

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)

I've now got a requirement that some of those futures don't run at the same time, but which ones can't be detemined until they've got the info above.

Is the right solution some sort of mutexes here? Say info (or some part of it) was a string, and 2 futures with the same string shouldn't run at the same time. How would I go about coding that?

解决方案

This can be done with a dictionary of locks, using the code below.

locks = defaultdict(threading.Lock)
main_lock = threading.Lock() 

with main_lock:
    lock = locks[info]
with lock:
    do_some_action(connection, value)

I'm not sure if the main lock is necessary, but this seems to work.

这篇关于在 Python 中使用具有并发期货的互斥锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆