在Python中正确使用互斥锁 [英] Proper use of mutexes in Python

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

问题描述

我从python中的多线程开始(或者至少我的脚本有可能创建多个线程).该算法是否是Mutex的正确用法?我尚未测试此代码,它甚至可能无法正常工作.我只希望processData在一个线程中运行(一次一个),而主while循环保持运行,即使队列中有一个线程也是如此.

I am starting with multi-threads in python (or at least it is possible that my script creates multiple threads). would this algorithm be the right usage of a Mutex? I haven't tested this code yet and it probably won't even work. I just want processData to run in a thread (one at time) and the main while loop to keep running, even if there is a thread in queue.

from threading import Thread
from win32event import CreateMutex
mutex = CreateMutex(None, False, "My Crazy Mutex")
while(1)
    t = Thread(target=self.processData, args=(some_data,))
    t.start()
    mutex.lock()

def processData(self, data)
    while(1)
        if mutex.test() == False:
            do some stuff
            break

重新阅读我的代码,我可以看到它是完全错误的.但是,这就是为什么我在这里寻求帮助.

re-reading my code I can see that it is grossly wrong. but hey, that's why I am here asking for help.

推荐答案

我不知道您为什么使用Window的Mutex而不是Python的Mutex.使用Python方法,这非常简单:

I don't know why you're using the Window's Mutex instead of Python's. Using the Python methods, this is pretty simple:

from threading import Thread, Lock

mutex = Lock()

def processData(data):
    mutex.acquire()
    try:
        print('Do some stuff')
    finally:
        mutex.release()

while True:
    t = Thread(target = processData, args = (some_data,))
    t.start()

但是请注意,由于CPython的体系结构(即全局解释器锁定),无论如何,实际上一次只能运行一个线程-如果其中许多线程是受I/O约束的,这很好,尽管您希望尽可能多地释放锁,以使不受I/O约束的线程不会阻止其他线程运行.

But note, because of the architecture of CPython (namely the Global Interpreter Lock) you'll effectively only have one thread running at a time anyway--this is fine if a number of them are I/O bound, although you'll want to release the lock as much as possible so the I/O bound thread doesn't block other threads from running.

对于Python 2.6和更高版本,一种替代方法是使用Python的multiprocessing包.它镜像threading程序包,但将创建可以同时运行的全新进程.更新您的示例很简单:

An alternative, for Python 2.6 and later, is to use Python's multiprocessing package. It mirrors the threading package, but will create entirely new processes which can run simultaneously. It's trivial to update your example:

from multiprocessing import Process, Lock

mutex = Lock()

def processData(data):
    with mutex:
        print('Do some stuff')

if __name__ == '__main__':
    while True:
        p = Process(target = processData, args = (some_data,))
        p.start()

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

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