【问题标题】:Sharing queue between threads running in different modules在不同模块中运行的线程之间共享队列
【发布时间】:2015-09-15 12:34:35
【问题描述】:

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

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

# 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)

【问题讨论】:

    标签: python multithreading python-2.7 queue


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      相关资源
      最近更新 更多