First, I running the code like that, and the retry is working properly.
# -*- coding:utf-8 -*-
from retrying import retry
import asyncio
import time
num = 0;
def retry_if_result_none(result):
return result is None
@retry(retry_on_result=retry_if_result_none)
def get_result():
global num;
num += 1;
if num < 10:
print('Retry.....');
return None;
else:
return True;
time.sleep(1);
def call():
end = get_result();
if end:
print('ok');
else:
print('over')
if __name__ == '__main__':
call();
Output:
Retry.....
Retry.....
Retry.....
Retry.....
Retry.....
Retry.....
Retry.....
Retry.....
Retry.....
ok
Second, I edit the code like that, and running again, but receive difference result.
# -*- coding:utf-8 -*-
from retrying import retry
import asyncio
import time
num = 0;
def retry_if_result_none(result):
# print("retry_if_result_none")
return result is None
@retry(retry_on_result=retry_if_result_none)
async def get_result():
global num;
num += 1;
if num < 10:
print('Retry.....');
return None;
else:
return True;
time.sleep(1);
async def call():
end = await get_result();
if end:
print('ok');
else:
print('over')
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(call())
Output:
Retry.....
over
As the show,the retry doesn't work in the second code. The difference is that I put the call() in loop.run_until_complete method, How to resolve this issue?