Quantcast
Channel: 月小升博客 »锁
Viewing all articles
Browse latest Browse all 2

python多线程 锁的使用

$
0
0
python的锁可以独立提取出来 mutex = threading.Lock() 锁的使用 #创建锁 mutex = threading.Lock() #锁定 mutex.acquire([timeout]) #释放 mutex.release() 锁定方法acquire可以有一个超时时间的可选参数timeout。如果设定了timeout,则在超时后通过返回值可以判断是否得到了锁,从而可以进行一些其他的处理。 #!/usr/bin/env python #coding=utf-8 import threading import time   class MyThread(threading.Thread): def run(self): global num time.sleep(1)   if mutex.acquire(1): num = num+1 msg = self.name+' set num … 继续阅读

Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images