我是Tornado和Python Threads的新手.我想要实现的目标如下:我有一个Tornado Web服务器,它接收用户的请求.我想在本地存储一些数据,并将其定期写入数据库作为批量插入.
import tornado.ioloop
import tornado.web
import threading
# Keep userData locally in memory
UserData = {}
def background(f):
"""
a threading decorator
use @background above the function you want to thread
(run in the background)
"""
def bg_f(*a, **kw):
threading.Thread(target=f, args=a, kwargs=kw).start()
return bg_f
@background
def PostRecentDataToDBThread(iter = -1):
i = 0
while iter == -1 or i < iter:
#send data to DB
UserData = {}
time.sleep(5*60)
i = i + 1
class AddHandler(tornado.web.RequestHandler):
def post(self):
userID …Run Code Online (Sandbox Code Playgroud)