Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.6.9 twisted:如何等待异步函数返回x,然后继续_Python_Async Await_Twisted_Deferred - Fatal编程技术网

Python3.6.9 twisted:如何等待异步函数返回x,然后继续

Python3.6.9 twisted:如何等待异步函数返回x,然后继续,python,async-await,twisted,deferred,Python,Async Await,Twisted,Deferred,我需要使用twisted运行python 3.6.9 我有一个函数,它需要等待async事件返回特定值才能继续。此异步事件始终返回此值,但可能需要时间 最终,我希望这样做(伪代码示例): 显然,这可以通过等待和推迟来实现,但我无法让它发挥作用。这里的根本问题是API_响应总是返回x和y,但顺序取决于情况。因此,等待API_response()==x您可能对while循环感兴趣: def do_stuff(): print('I need API to say hello to me')

我需要使用twisted运行python 3.6.9

我有一个函数,它需要等待async事件返回特定值才能继续。此异步事件始终返回此值,但可能需要时间

最终,我希望这样做(伪代码示例):


显然,这可以通过等待和推迟来实现,但我无法让它发挥作用。这里的根本问题是API_响应总是返回x和y,但顺序取决于情况。因此,等待API_response()==x

您可能对while循环感兴趣:

def do_stuff():

   print('I need API to say hello to me')
   do something that triggers API_response
   while await until API_response() != x:
       # Delay until you think API_response might be different
   print('success!')

当然,这个代码并不比问题中的代码更有效。我假设有一些有效的原始代码可以以相同的方式进行转换,并且只编辑此伪代码以显示
while
循环可能会有帮助。

您可能对while循环感兴趣:

def do_stuff():

   print('I need API to say hello to me')
   do something that triggers API_response
   while await until API_response() != x:
       # Delay until you think API_response might be different
   print('success!')

当然,这个代码并不比问题中的代码更有效。我假设有一些有效的原始代码可以用同样的方式进行转换,并且只编辑这个伪代码来显示
while
循环可能会有帮助。

伪伪代码使您很难理解您要做的事情。我不知道你说的“触发器”是什么意思。这和“电话”不同吗?我会粗略地猜一猜答案,但是你可能想考虑熟悉自己,伪伪代码让你很难跟上你想要做的事情。我不知道你说的“触发器”是什么意思。这和“电话”不同吗?我会粗略地猜一猜答案,但你可能想考虑熟悉自己。