This is my code:
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()
So worker
is the function started by Multiprocessing and it's trying to call function getCookie()
which uses a function by Flask
.
It seems to throw me an error saying
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.
Anyway I can fix this?
UPDATE
Also, Ive tried sending the request function in the args like so
from flask import Flask, render_template, url_for, redirect, request, flash, Markup, make_response, Response
from multiprocessing import Process, Value
def worker(state, req):
if state.value == 1:
while True:
print(req.cookies.get('username'))
if __name__ == '__main__':
processValues = Value('i', 1)
p = Process(target=worker, args=(processValues, request ))
p.start()
app.run()
p.join()
And this just throws me another error.
if you need to only read this cookie then you could send it as argument
p = Process(target=worker, args=(processValues, getCookie()))
or directly
p = Process(target=worker, args=(processValues, request.cookies.get('username'))
But you have to get it in worker as
def worker(state, username):
Similar way you could try to send full request
p = Process(target=worker, args=(processValues, request))
BTW: as I know multiprocessing
uses pickle
to send arguments as file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.