I am trying to build a server to handle long time task job, so I made a global variable tasks
so that request can easily returned by only put task info into tasks
, and I using threading to build a function to handle the long time task job.
however I can't receive the tasks
change in test()
, why had this happen?
import time
import threading
from collections import OrderedDict
tasks = OrderedDict()
def request():
# network gross
# ...
global tasks
tasks['zdx'] = 2
def test():
print('test runing')
while True:
if tasks:
task = tasks.popitem()
print('I get the source!')
# very long time resolve task
time.sleep(1)
def init():
threading.Thread(target=test, daemon=True).start()
init()
time.sleep(3)
request()