如何将数据发送到正在运行的python线程?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何将数据发送到正在运行的python线程?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3389字,纯文字阅读大概需要5分钟。
内容图文
我有一个在我的应用程序中的不同线程中运行的类.我可以一次运行多个线程,线程是守护进程.一段时间后,其中一些线程需要接收和处理消息.我该怎么做呢?
我的代码示例如下所示:
import threading
import time
class MyThread(threading.Thread):
def __init__(self, args=(), kwargs=None):
threading.Thread.__init__(self, args=(), kwargs=None)
self.daemon = True
self.receive_messages = args[0]
def run(self):
print threading.currentThread().getName(), self.receive_messages
def do_thing_with_message(self, message):
if self.receive_messages:
print threading.currentThread().getName(), "Received %s".format(message)
if __name__ == '__main__':
threads = []
for t in range(10):
threads.append( MyThread(args=(t % 2 == 0,)))
threads[t].start()
time.sleep(0.1)
for t in threads:
t.do_thing_with_message("Print this!")
这输出:
Thread-1 True
Thread-2 False
Thread-3 True
Thread-4 False
Thread-5 True
Thread-6 False
Thread-7 True
Thread-8 False
Thread-9 True
Thread-10 False
MainThread Received %s
MainThread Received %s
MainThread Received %s
MainThread Received %s
MainThread Received %s
但是,我期待最后五行不与MainThread相关,而不是%s,我希望它能打印出来!,就像这样:
Thread-1 True
Thread-2 False
Thread-3 True
Thread-4 False
Thread-5 True
Thread-6 False
Thread-7 True
Thread-8 False
Thread-9 True
Thread-10 False
Thread-1 Received Print this!
Thread-3 Received Print this!
Thread-5 Received Print this!
Thread-7 Received Print this!
Thread-9 Received Print this!
如何正确地将这样的消息发送到正在运行的线程?
附录:
如果我在打印之后有这个块!阻止,并利用@ dano的代码来解决上面的问题,它似乎没有响应这些新消息.
for t in threads:
t.queue.put("Print this again!")
time.sleep(0.1)
在这种情况下,我希望我的输出结束看起来像这样
Thread-1 Received Print this!
Thread-3 Received Print this!
Thread-5 Received Print this!
Thread-7 Received Print this!
Thread-9 Received Print this!
Thread-1 Received Print this again!
Thread-3 Received Print this again!
Thread-5 Received Print this again!
Thread-7 Received Print this again!
Thread-9 Received Print this again!
解决方法:
您可以使用Queue.Queue(或Python 3中的queue.Queue):
import threading
import time
from Queue import Queue
print_lock = threading.Lock()
class MyThread(threading.Thread):
def __init__(self, queue, args=(), kwargs=None):
threading.Thread.__init__(self, args=(), kwargs=None)
self.queue = queue
self.daemon = True
self.receive_messages = args[0]
def run(self):
print threading.currentThread().getName(), self.receive_messages
val = self.queue.get()
self.do_thing_with_message(val)
def do_thing_with_message(self, message):
if self.receive_messages:
with print_lock:
print threading.currentThread().getName(), "Received {}".format(message)
if __name__ == '__main__':
threads = []
for t in range(10):
q = Queue()
threads.append(MyThread(q, args=(t % 2 == 0,)))
threads[t].start()
time.sleep(0.1)
for t in threads:
t.queue.put("Print this!")
for t in threads:
t.join()
我们将Queue实例传递给每个线程,并使用queue.put将消息发送到Thread.我们等待消息到达run方法,该方法是Thread对象的一部分,它实际上是在一个单独的执行线程中运行的.收到消息后,我们调用do_thing_with_message,它将在同一个后台线程中运行.
我还在代码中添加了一个threading.Lock,因此stdout的打印件不会混淆.
编辑:
如果您希望能够向线程传递多条消息,只需使用循环:
def run(self):
print threading.currentThread().getName(), self.receive_messages
while True:
val = self.queue.get()
if val is None: # If you send `None`, the thread will exit.
return
self.do_thing_with_message(val)
内容总结
以上是互联网集市为您收集整理的如何将数据发送到正在运行的python线程?全部内容,希望文章能够帮你解决如何将数据发送到正在运行的python线程?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。