python - 我可以在 python 线程中使用全局队列吗?
问题描述
我希望将项目放入线程中的全局队列中。可能吗?
这是伪代码:
def parse_a_file():
global sql_qool
sql_qool.put(xxx)
sql_qool = Queue.Queue()
t = threading.Thread(target=parse_a_file)
t.setDaemon(True)
t.start()
解决方案
Python 2 的更新
import Queue
from time import sleep
import threading
def parse_a_file():
# the following global is not really required
#global sql_qool
sql_qool.put('xxx')
print 'I put xxx.'
sql_qool = Queue.Queue()
t = threading.Thread(target=parse_a_file)
t.setDaemon(True)
t.start()
sleep(1) # give daemon thread a chance to run
印刷:
I put xxx.
推荐阅读
- python - python django从函数向会话添加变量并同时从另一个视图函数访问它
- ios - UIcollectionView - 创建填充项目网格的水平滚动寻呼机
- python - 带有 python 加密模块的 InvalidToken
- r - 无法在 Shiny DT 中隐藏列并设置行名 = FALSE
- android - 这是我后台服务的哪一部分不正确?
- jq - 使用 try catch 更新数组元素
- python - 如何在列表中选择特定的最小值并对它们进行数学运算
- c - 自由导致C中的分段错误
- css - 与 Css Translate 一起使用时来自 getBoundingClientRect 的 X 和 Y 值错误
- javascript - AG-Grid 区分行选择