Reputation: 11
I need to run python 3.6.9 with twisted.
I have a function which needs to wait for async event to return certain value in order to continue. This async event always return this value , but it may take time.
Ultimately I would want to do this (pseudo code example):
def API_response(msg):
if msg == 'hello':
return x
if msg == 'world':
return y
def do_stuff():
print('I need API to say hello to me')
do something that triggers API_response
await until API_response() == x
print('success!')
start socket managers
run socket managers which will eventually trigger do_stuff()
apparently this can be achieved with await and deferreds, but I cant get it to work. And the underlying problem here is that API_response will always return x and y, but order depends on situation. Hence await until API_response() == x
Upvotes: 1
Views: 498
Reputation: 48335
You may be interested in a while loop:
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!')
Of course, this code is no more valid than the code in the question. I've assumed there is some valid original code which could be transformed in the same way and only edited this pseudo-pseudo-code to show how a while
loop might be helpful.
Upvotes: 1