简单线程事件示例 [英] Simple threading event example

查看:64
本文介绍了简单线程事件示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Python 3的线程文档,但是我在理解材料时遇到了麻烦.

I'm reading Python 3's Threading documentation but I'm having trouble understanding the material.

我有两个计时器可以并排运行,但是我不知道如何使这两个线程彼此交互.锁定,传递事件或其他任何东西.

I've gotten two timers to run side by side but I don't know how to make these two threads interact with each other. Lock, pass an event or anything.

有人可以在这里转储一个简单的完整示例,并简要说明其功能吗?

Could somebody just dump a simple full example here with brief description of what it does?

我正在尝试学习3.3,因此,如果可能的话,您将能够发布适用于该版本的代码.我还发现我找到的教程并没有让我知道他们正在测试的是哪个版本的Python.

I'm trying to learn 3.3 so if possible would you be able to post code that works for this version. I'm also finding that the tutorials I find aren't letting me know what version of Python they're exampling.

推荐答案

基于queue的示例

输出:

Thread-3 2
Thread-1 0
Thread-2 1
Thread-4 3
Thread-3 4
Thread-1 5
Thread-2 6
Thread-4 7
Thread-3 8
Thread-1 9
Thread-2 10
Thread-4 11
Thread-3 12
Thread-1 13
Thread-2 14
Thread-4 15
Thread-1 17
Thread-3 16
Thread-2 18
Thread-4 19
time: 0.5017914706686906

这篇关于简单线程事件示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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