[英]How can I fix coroutine was never awaited?
I have a RESTFUL Flask API I am serving with gunicorn and I'm trying to continue running parse_request()
after sending a response to whoever made a POST request so they're not left waiting for it to finish 我有一个RESTIFUL Flask API,我正在使用gunicorn,我正在尝试继续运行
parse_request()
之后向发出POST请求的人发送响应,这样他们就不会等待它完成
I'm not too sure if this will even achieve what I want but this is the code I have so far. 我不太确定这是否会达到我想要的效果,但这是我到目前为止的代码。
from threading import Thread
import subprocess
from flask import Flask
import asyncio
application = Flask(__name__)
async def parse_request(data):
try:
command = './webscraper.py -us "{user}" -p "{password}" -url "{url}"'.format(**data)
output = subprocess.check_output(['bash','-c', command])
except Exception as e:
print(e)
@application.route('/scraper/run', methods=['POST'])
def init_scrape():
try:
thread = Thread(target=parse_request, kwargs={'data': request.json})
thread.start()
return jsonify({'Scraping this site: ': request.json["url"]}), 201
except Exception as e:
print(e)
if __name__ == '__main__':
try:
application.run(host="0.0.0.0", port="8080")
except Exception as e:
print(e)
I am sending a POST request similar to this. 我正在发送类似于此的POST请求。
localhost:8080/scraper/run
data = {
"user": "username",
"password": "password",
"url": "www.mysite.com"
}
The error I get when sending a POST request is this. 发送POST请求时收到的错误是这个。
/usr/lib/python3.6/threading.py:864: RuntimeWarning: coroutine 'parse_request' was never awaited
self._target(*self._args, **self._kwargs)
So first things first, why are you calling webscraper.py with subprocess? 首先,为什么要用子进程调用webscraper.py? This is completely pointless.
这完全没有意义。 Because webscraper.py is a python script you should be importing the needed functions/classes from webscraper.py and using them directly.
因为webscraper.py是一个python脚本,所以你应该从webscraper.py导入所需的函数/类并直接使用它们。 Calling it this way is totally defeating what you are wanting to do.
以这种方式调用它完全打败了你想要做的事情。
Next, your actual question you have got mixed up between async and threading. 接下来,您在异步和线程之间混淆了实际问题。 I suggest you learn more about it but essentially you want something like the following using Quart which is an async version of Flask, it would suit your situation well.
我建议你了解更多关于它的内容,但基本上你想要的东西如下使用Quart这是Flask的异步版本,它会很好地适合你的情况。
from quart import Quart, response, jsonify
import asyncio
from webscraper import <Class>, <webscraper_func> # Import what you need or
import webscraper # whatever suits your needs
app = Quart(__name__)
async def parse_request(user, password, url):
webscraper_func(user, password, url)
return 'Success'
@app.route('/scraper/run', methods=['POST'])
async def init_scrape():
user = request.args.get('user')
password = request.args.get('password')
url = request.args.get('url')
asyncio.get_running_loop().run_in_executor(
None,
parse_request(user, password, url)
)
return 'Success'
if __name__ == '__main__':
app.run(host='0.0.0.0', port='8080')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.