作者:o0沢田纲吉0o | 来源:互联网 | 2023-09-17 17:55
这是我的代码:
from flask import flask,render_template,url_for,redirect,request,flash,Markup,make_response,Response
from multiprocessing import Process,Value
def getCOOKIE():
return request.COOKIEs.get('username')
def worker(state):
if state.value == 1:
while True:
print(getCOOKIE())
if __name__ == '__main__':
processValues = Value('i',1)
p = Process(target=worker,args=(processValues,))
p.start()
app.run()
p.join()
因此worker
是由Multiprocessing启动的函数,它正在尝试调用getCOOKIE()
使用此函数的函数flask
。
似乎给我说了一个错误
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
我能解决这个问题吗?
更新
此外,我尝试像这样在args中发送请求函数
from flask import flask,Value
def worker(state,req):
if state.value == 1:
while True:
print(req.COOKIEs.get('username'))
if __name__ == '__main__':
processValues = Value('i',request ))
p.start()
app.run()
p.join()
这只会引发另一个错误。
如果您只需要读取此COOKIE,则可以将其作为参数发送
p = Process(target=worker,args=(processValues,getCOOKIE()))
或直接
p = Process(target=worker,request.COOKIEs.get('username'))
但是您必须以
的身份获得它
def worker(state,username):
您可以尝试发送完整的request
p = Process(target=worker,request))
顺便说一句:据我所知multiprocessing
使用pickle
将参数作为文件发送。