【发布时间】:2016-09-09 13:15:04
【问题描述】:
我在烧瓶中编写一个 Web 应用程序,显示从 api 中提取的巴士站。
我在 index.html 上有一个表单,用户可以在其中输入停止编号,该编号会在 views.py 中获取,该函数还通过 celery 运行任务以获取 api 数据:
from flask import render_template, request
from app import app
@app.route('/')
def index():
return render_template('index.html')
@app.route('/stopno', methods=['POST'])
def stopno():
stopid = request.form['input']
from app.tasks import apidata
apidata.delay()
return render_template('action.html')
这是我的tasks.py:
from celery import Celery
import json
import requests
import time
ac = Celery('tasks', broker='amqp://localhost')
@ac.task(name='tasks.apidata')
def apidata():
from views import stopno
api = '*apiurl*:' + str(stopid)
saveinfo = 'static/stop' + str(stopid)+ '.json'
r = requests.get(api)
jsondata = json.loads(r.text)
with open(saveinfo, 'w') as f:
json.dump(jsondata, f)
我正在将视图中的 stopno 导入到任务中,因此我可以在 api 中搜索指定的停靠点,当前当用户输入停靠点编号时,action.html 加载正常并显示用户输入的停靠点编号,但没有新文件或数据是为停止号创建的,并且 celery 会抛出错误提示
ImportError: No module named views
我的项目结构是
|____run.py
|____app
| |______init__.py
| |____views.py
| |____tasks.py
| |____static
| | |____json
| |____templates
| | |____action.html
| | |____index.html
【问题讨论】:
-
试试
from app.views import stopno。 -
已经试过了,抛出同样的错误。
标签: python module celery python-import celery-task