在不同模块中运行的线程之间共享队列 [英] Sharing queue between threads running in different modules

查看:41
本文介绍了在不同模块中运行的线程之间共享队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目在不同的包中有一些模块.这个项目需要几个线程,可能会在不同的模块中启动,我打算使用队列进行线程间通信.

I have some modules in different packages for my project. This project requires several threads which might be started in different modules, and I intend to use queues for the inter-thread communication.

有没有办法传递在一个模块中创建的队列以供另一个模块使用?

Is there a way to pass a queue created in one module for use in another module?

# ModuleA.py    

myQueue = Queue.Queue()
thread = myThread(threadID1, tName1, myQueue)
thread2 = myThread(threadID2, tName2, myQueue)

# ModuleB.py

myQueue = get_the_previous_queue_created() # possible?
thread3 = myThread(threadID3, tName3, myQueue)

推荐答案

队列与任何其他对象没有什么不同,因为如果它是在一个模块中的模块级别定义的,您可以导入该模块并直接访问它:

A Queue is not different from any other object, in that if it is defined at module level in one module you can import that module and access it directly:

import ModuleA
# now ModuleA.myQueue is the Queue object created there

这篇关于在不同模块中运行的线程之间共享队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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