, 내 셀러리 완벽하게 실행되고, 여기 내 코드는 있습니다플라스크에서 셀러리를 사용하여 세션에 값을 추가하는 방법은 무엇입니까?
from flask import session, g
from ..extensions import celery
CELERYBEAT_SCHEDULE = {
'add-every-10-seconds': {
'task': 'tasks.ruble',
'schedule': timedelta(seconds=20)
}
}
@celery.task(name='tasks.ruble')
def ruble():
url = 'https://finance.google.com/finance/converter?a=1&from=KGS&to=RUB&meta=ei%3DmSr0WeHCCYvBsAH8n6OIBA'
urlHandler = urllib2.urlopen(url)
html = urlHandler.read()
bsoup = BeautifulSoup(html, 'lxml')
num = bsoup.find('span').text.split()[0]
g.get('ruble', float(num))
session['get_ruble'] = float(num)
하지만 시간이 실행 올 때 나는 또한 그냥 그렇게 g.ruble = float(num)
에 float(num)
값을 할당하여, 대신 세션의 g
을 사용하려고한다는
[2017-10-30 19:19:29,372: ERROR/PoolWorker-4] Task tasks.ruble[ff7617ad-764b-455c-b541-96f3ba78a87b] raised unexpected: RuntimeError('Working outside of request context.\n\nThis typically means that you attempted to use functionality that needed\nan active HTTP request. Consult the documentation on testing for\ninformation about how to avoid this problem.',)
Traceback (most recent call last):
File "/home/xakep/Desktop/work/optomvse/venv/local/lib/python2.7/site-packages/celery/app/trace.py", line 367, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/xakep/Desktop/work/optomvse/celery_worker.py", line 21, in __call__
return TaskBase.__call__(self, *args, **kwargs)
File "/home/xakep/Desktop/work/optomvse/venv/local/lib/python2.7/site-packages/celery/app/trace.py", line 622, in __protected_call__
return self.run(*args, **kwargs)
File "/home/xakep/Desktop/work/optomvse/develop/tasks.py", line 34, in ruble
session['get_ruble'] = float(num)
File "/home/xakep/Desktop/work/optomvse/venv/local/lib/python2.7/site-packages/werkzeug/local.py", line 350, in __setitem__
self._get_current_object()[key] = value
File "/home/xakep/Desktop/work/optomvse/venv/local/lib/python2.7/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/home/xakep/Desktop/work/optomvse/venv/local/lib/python2.7/site-packages/flask/globals.py", line 37, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
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.
내가 얘기를 깜빡 했네요 또 다른 한가지는,하지만 내 템플릿 내부 : 작업, 그것은이 오류와 함께 끝 나는 나는 {{g.ruble}}
을 입력하지 않았는데 아무 것도 출력되지 않습니다.
이 코드를 어떻게 부르나요? 요청 컨텍스트를 수정하려면 비동기 작업이 아닌 동기식으로 수행해야하는 작업 일 수 있습니다. 다른 웹 사이트에서 주기적으로 데이터를 가져 오려면 redis/memcached 또는 세션 객체가 아닌 파일에 데이터를 데이터베이스에 저장할 수 있습니다. –
@ HåkenLid, 관심 메이트 주셔서 감사합니다. 통화 가치가 매번 바뀌고 있기 때문에 세션에 가치를 저장하고 싶습니다. 세션에서 절약하면 더 충분할 것입니다! – swordfish
템플릿의 값에 액세스해야하는 경우 각 요청에 대해 자동으로 가져올 수 있습니다. 그래서 전혀 셀러리를 사용하지 않고. –