【发布时间】:2020-04-13 13:45:17
【问题描述】:
我正在尝试定义此发送消息功能。但是有什么问题..
import asyncio
import websockets
async def send_msg(msg):
async with websockets.client.Connect(url) as websocket:
print("Connected to WebSocket!")
await websocket.send(msg)
send_msg('hi')
loop = asyncio.get_event_loop()
task = loop.create_task(send_msg())
loop.run_until_complete(task)
TypeError: send_msg() missing 1 required positional argument: 'msg'
有人可以帮我吗?谢谢
【问题讨论】:
-
task = loop.create_task(send_msg())怎么样?它不应该包含msg参数吗? -
task = loop.create_task(send_msg(msg)) NameError: name 'msg' is not defined 这是我更改后得到的
-
你需要传递一个字符串,例如
task = loop.create_task(send_msg("hey!")) -
天哪,你是对的!但是在将它导入另一个之后我该如何使用它呢?我需要继续使用最后两行吗?
-
“在另一个中导入此内容后”到底是什么意思?
标签: python python-asyncio