Python 如何从异步函数返回值
我有一个异步函数,我正试图从中获取返回变量,但由于某种原因,我无法让它工作。我尝试了谷歌搜索的一些不同方法,但它们都返回了类似的错误 我有这个功能:Python 如何从异步函数返回值,python,python-3.x,async-await,return,discord,Python,Python 3.x,Async Await,Return,Discord,我有一个异步函数,我正试图从中获取返回变量,但由于某种原因,我无法让它工作。我尝试了谷歌搜索的一些不同方法,但它们都返回了类似的错误 我有这个功能: @bot.command() async def start(ctx): """starts the server""" try: status = server.status() await ctx.channel.send("The server is already online!")
@bot.command()
async def start(ctx):
"""starts the server"""
try:
status = server.status()
await ctx.channel.send("The server is already online!")
except:
os.chdir(".\\Minecraft")
file = subprocess.Popen("Start.bat")
await ctx.channel.send("starting the server")
starting = True
while starting == True:
time.sleep(int(delay))
with open("outfile.txt") as outfile:
for line in outfile:
if "Done" in line:
await ctx.channel.send("server has loaded")
starting = False
return file
else:
continue
然后我返回变量文件
但是当我尝试在另一个函数中获取变量时
@bot.command()
async def kill(ctx):
"""shuts down the server"""
x = start(ctx)
print(x)
x.terminate()
我得到一个错误:
<coroutine object Command.__call__ at 0x040F7B28>
Ignoring exception in command kill:
Traceback (most recent call last):
File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "c:/Users/TheRi/OneDrive/Desktop/Python/MinecraftDiscordBot/minecraftBot.py", line 113, in kill
x.terminate()
AttributeError: 'coroutine' object has no attribute 'terminate'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'coroutine' object has no attribute 'terminate'
C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\asyncio\events.py:81: RuntimeWarning: coroutine 'Command.__call__' was never awaited
self._context.run(self._callback, *self._args)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
忽略命令kill中的异常:
回溯(最近一次呼叫最后一次):
文件“C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site packages\discord\ext\commands\core.py”,第83行,已包装
ret=等待coro(*args,**kwargs)
kill中第113行的文件“c:/Users/TheRi/OneDrive/Desktop/Python/MinecraftDiscordBot/minecraftBot.py”
x、 终止()
AttributeError:'coroutine'对象没有属性'terminate'
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site packages\discord\ext\commands\bot.py”,第892行,在invoke中
等待ctx.command.invoke(ctx)
文件“C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site packages\discord\ext\commands\core.py”,第797行,在invoke中
等待注入(*ctx.args,**ctx.kwargs)
文件“C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\site packages\discord\ext\commands\core.py”,第92行,已包装
从exc引发CommandInvokeError(exc)
discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:“协同路由”对象没有属性“终止”
C:\Users\TheRi\AppData\Local\Programs\Python\Python38-32\lib\asyncio\events.py:81:RuntimeWarning:coroutine命令。从未等待过调用
self.\u context.run(self.\u回调,*self.\u参数)
RuntimeWarning:启用tracemalloc以获取对象分配回溯
第一行似乎是我试图打印x的地方,看看是否能看到发生了什么,剩下的就是错误。它似乎没有返回任何值,只是返回co例程本身
我尝试过改变引用函数的方式,x=start(ctx),x=start(),x=start等等
我做错什么了吗?如何返回变量。您需要等待协同程序;i、 e.
x=等待启动(ctx)