我试图写一个请求处理程序,以帮助我送在异步模式下请求。 它提示当我关闭用Ctrl + d或退出Python终端()
它显示sys:1: RuntimeWarning: coroutine was never awaited
import asyncio
import urllib.request
import json
class RequestHandler:
def SendPostRequest(method="post",url=None, JsonFormatData={}):
# Encode JSON
data =json.dumps(JsonFormatData).encode('utf8')
# Config Request Header
req = urllib.request.Request(url)
req.add_header('Content-Type', 'application/json')
# Send request and wait the response
response = urllib.request.urlopen(req,data=data)
return response
async def AsyncSend(method="post",url=None, JsonFormatData=None):
if method == "post":
loop = asyncio.get_event_loop()
task = loop.create_task(SendPostRequest(method="post",url=url,JsonFormatData=JsonFormatData))
###################################
# Example
##### In main python terminal, i run like this:
# from RequestHandler import *
# RequestHandler.AsyncSend(method="post",url="xxxxxx", JsonFormatData={'key':'value'} )
当我按Ctrl + d,它提示
sys:1: RuntimeWarning: coroutine 'RequestHandler.AsyncSend' was never awaited
那是我应该忽略它? 我不想叫await
,因为我不关心,如果这个过程是成功与否。
在这个环节“ https://xinhuang.github.io/posts/2017-07-31-common-mistakes-using-python3-asyncio.html ”,它说:“要没有的await执行异步任务,使用循环。 create_task()与loop.run_until_complete()”,是错的呢?