- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python中使用Queue和Condition进行线程同步的方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
Queue模块保持线程同步 利用Queue对象先进先出的特性,将每个生产者的数据一次存入队列,而每个消费者将依次从队列中取出数据 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import
threading
# 导入threading模块
import
Queue
# 导入Queue模块
class
Producer(threading.Thread):
# 定义生产者类
def
__init__(
self
,threadname):
threading.Thread.__init__(
self
,name
=
threadname)
def
run(
self
):
global
queue
# 声明queue为全局变量
queue.put(
self
.getName())
# 调用put方法将线程名添加到队列中
print
self
.getName(),
'put '
,
self
.getName(),
' to queue'
class
Consumer(threading.Thread):
# 定义消费者类
def
__init__(
self
,threadname):
threading.Thread.__init__(
self
,name
=
threadname)
def
run(
self
):
global
queue
print
self
.getName(),
'get '
,queue.get(),
'from queue'
#调用get方法获取队列中内容
queue
=
Queue.Queue()
# 生成队列对象
plist
=
[]
# 生成者对象列表
clist
=
[]
# 消费者对象列表
for
i
in
range
(
10
):
p
=
Producer(
'Producer'
+
str
(i))
plist.append(p)
# 添加到生产者对象列表
for
i
in
range
(
10
):
c
=
Consumer(
'Consumer'
+
str
(i))
clist.append(c)
# 添加到消费者对象列表
for
i
in
plist:
i.start()
# 运行生产者线程
i.join()
for
i
in
clist:
i.start()
# 运行消费者线程
i.join()
######运行结果######
>>> Producer0 put Producer0 to queue
Producer1 put Producer1 to queue
Producer2 put Producer2 to queue
Producer3 put Producer3 to queue
Producer4 put Producer4 to queue
Producer5 put Producer5 to queue
Producer6 put Producer6 to queue
Producer7 put Producer7 to queue
Producer8 put Producer8 to queue
Producer9 put Producer9 to queue
Consumer0 get Producer0
from
queue
Consumer1 get Producer1
from
queue
Consumer2 get Producer2
from
queue
Consumer3 get Producer3
from
queue
Consumer4 get Producer4
from
queue
Consumer5 get Producer5
from
queue
Consumer6 get Producer6
from
queue
Consumer7 get Producer7
from
queue
Consumer8 get Producer8
from
queue
Consumer9 get Producer9
from
queue
|
Condition实现复杂的同步 使用Condition对象可以在某些事件触发或者达到特定的条件后才处理数据,Condition除了具有Lock对象的acquire方法和release方法外, 还有wait方法,notify方法,notifyAll方法等用于条件处理。 条件变量保持线程同步:threading.Condition() 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#coding:utf-8
import
threading
import
time
cond
=
threading.Condition()
class
kongbaige(threading.Thread):
def
__init__(
self
, cond, diaosiname):
threading.Thread.__init__(
self
, name
=
diaosiname)
self
.cond
=
cond
def
run(
self
):
self
.cond.acquire()
#获取锁
print
self
.getName()
+
':一支穿云箭'
#空白哥说的第一句话
self
.cond.notify()
#唤醒其他wait状态的线程(通知西米哥 让他说话)
#然后进入wait线程挂起状态等待notify通知(等西米哥的回复,接下来俩人就开始扯蛋)
self
.cond.wait()
print
self
.getName()
+
':山无棱,天地合,乃敢与君绝!'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':紫薇!!!!(此处图片省略)'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':是你'
self
.cond.notify()
self
.cond.wait()
#这里是空白哥说的最后一段话,接下来就没有对白了
print
self
.getName()
+
':有钱吗 借点'
self
.cond.notify()
#通知西米哥
self
.cond.release()
#释放锁
class
ximige(threading.Thread):
def
__init__(
self
, cond, diaosiname):
threading.Thread.__init__(
self
, name
=
diaosiname)
self
.cond
=
cond
def
run(
self
):
self
.cond.acquire()
self
.cond.wait()
#线程挂起(等西米哥的notify通知)
print
self
.getName()
+
':千军万马来相见'
self
.cond.notify()
#说完话了notify空白哥wait的线程
self
.cond.wait()
#线程挂起等待空白哥的notify通知
print
self
.getName()
+
':海可枯,石可烂,激情永不散!'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':尔康!!!(此处图片省略)'
self
.cond.notify()
self
.cond.wait()
print
self
.getName()
+
':是我'
self
.cond.notify()
self
.cond.wait()
#这里是最后一段话,后面空白哥没接话了 所以说完就释放锁 结束线程
print
self
.getName()
+
':滚'
self
.cond.release()
kongbai
=
kongbaige(cond,
' '
)
ximi
=
ximige(cond,
'西米'
)
#尼玛下面这2个启动标志是关键,虽然是空白哥先开的口,但是不能让他先启动,
#因为他先启动的可能直到发完notify通知了,西米哥才开始启动,
#西米哥启动后会一直处于44行的wait状态,因为空白哥已经发完notify通知了进入wait状态了,
#而西米哥没收到
#造成的结果就是2根线程就一直在那挂起,什么都不干,也不扯蛋了
ximi.start()
kongbai.start()
|
######运行结果###### 。
1
2
3
4
5
6
7
8
9
10
|
:一支穿云箭
西米:千军万马来相见
:山无棱,天地合,乃敢与君绝!
西米:海可枯,石可烂,激情永不散!
:紫薇!!!!(此处图片省略)
西米:尔康!!!(此处图片省略)
:是你
西米:是我
:有钱吗 借点
西米:滚
|
最后此篇关于Python中使用Queue和Condition进行线程同步的方法的文章就讲到这里了,如果你想了解更多关于Python中使用Queue和Condition进行线程同步的方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在实现 IMAP 客户端,但 IMAP 邮箱同步出现问题。 首先,可以从 IMAP 服务器获取新邮件,但我不知道如何从邮箱中查找已删除的邮件。 我是否应该从服务器获取所有消息并将其与本地数据进行比
我研究线程同步。当我有这个例子时: class A { public synchronized void methodA(){ } public synchronized void met
嗨,我做了一个扩展线程的东西,它添加了一个包含 IP 的对象。然后我创建了该线程的两个实例并启动它们。他们使用相同的列表。 我现在想使用 Synchronized 来阻止并发更新问题。但它不起作用,我
我正在尝试使用 FTP 定期将小数据文件从程序上传到服务器。用户从使用 javascript XMLHttpRequest 函数读取数据的网页访问数据。这一切似乎都有效,但我正在努力解决由 FTP 和
我不知道如何同步下一个代码: javascript: (function() { var s2 = document.createElement('script'); s2.src =
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
一 点睛 1 Message 在基于 Message 的系统中,每一个 Event 也可以被称为 Message,Message 是对 Event 更高一个层级的抽象,每一个 Message 都有一个
一 点睛 1 Message 在基于 Message 的系统中,每一个 Event 也可以被称为 Message,Message 是对 Event 更高一个层级的抽象,每一个 Message 都有一个
目标:我所追求的是每次在数据库中添加某些内容时(在 $.ajax 到 Submit_to_db.php 之后),从数据库获取数据并刷新 main.php(通过 draw_polygon 更明显)。 所
我有一个重复动画,需要与其他一些 transient 动画同步。重复动画是一条在屏幕上移动 4 秒的扫描线。当它经过下面的图像时,这些图像需要“闪烁”。 闪烁的图像可以根据用户的意愿来来去去和移动。它
我有 b 个块,每个块有 t 个线程。 我可以用 __syncthreads() 同步特定块中的线程。例如 __global__ void aFunction() { for(i=0;i #
我正在使用azure表查询来检索分配给用户的所有错误实体。 此外,我更改了实体的属性以声明该实体处于处理模式。 处理完实体后,我将从表中删除该实体。 当我进行并行测试时,可能会发生查询期间,一个实体已
我想知道 SQLite 是如何实现它的。它基于文件锁定吗?当然,并不是每个访问它的用户都锁定了整个数据库;那效率极低。它是基于多个文件还是仅基于一个大文件? 如果有人能够简要概述一下 sqlite 中
我想post到php,当id EmpAgree1时,然后它的post变量EmpAgree=1;当id为EmpAgree2时,则后置变量EmpAgree=2等。但只是读取i的最后一个值,为什么?以及如何
CUBLAS 文档提到我们在读取标量结果之前需要同步: “此外,少数返回标量结果的函数,例如 amax()、amin、asum()、rotg()、rotmg()、dot() 和 nrm2(),通过引用
我知道下面的代码中缺少一些内容,我的问题是关于 RemoteImplementation 中的同步机制。我还了解到该网站和其他网站上有几个关于 RMI 和同步的问题;我在这里寻找明确的确认/矛盾。 我
我不太确定如何解决这个问题......所以我可能需要几次尝试才能正确回答这个问题。我有一个用于缓存方法结果的注释。我的代码目前是一个私有(private)分支,但我正在处理的部分从这里开始: http
我对 Java 非常失望,因为它不允许以下代码尽可能地并发移动。当没有同步时,两个线程会更频繁地切换,但是当尝试访问同步方法时,在第二个线程获得锁之前以及在第一个线程获得锁之前再次花费太长时间(比如
过去几周我一直在研究java多线程。我了解了synchronized,并理解synchronized避免了多个线程同时访问相同的属性。我编写此代码是为了在同一线程中运行两个线程。 val gate =
我有一个关于 Java 同步的简单问题。 请假设以下代码: public class Test { private String address; private int age;
我是一名优秀的程序员,十分优秀!