async def run(): fut = asyncio.ensure_future(update_check()) loop = asyncio.get_event_loop() await loop.run_in_executor(None, run_main) update_version = await fut if update_version is not None: print(f"A new release of gh is available: xxx → {update_version}")
async def update_check() -> t.Awaitable[t.Optional[str]]: print("-> update_check ...") await asyncio.sleep(0.5) print("<- ... update_check") if random.random() < 0.2: # update is not found return None # update is found return "0.8.8"
def update_check() -> t.Optional[str]: print("-> update_check ...") time.sleep(0.5) print("<- ... update_check") if random.random() < 0.2: # update is not found return None # update is found return "0.8.8"
async def run(): fut = asyncio.ensure_future(update_check()) print("do something (main)") for i in range(6): print(".") sys.stdout.flush() await asyncio.sleep(0.1) print("ok") update_version = await fut if update_version is not None: print(f"A new release of gh is available: xxx → {update_version}")
def run_main(): import time print("do something (main)") for i in range(6): print(".") time.sleep(0.1) print("ok")
def main(): update_version = None def _check(): nonlocal update_version update_version = update_check() th = threading.Thread(target=_check, daemon=True) th.start() print("do something (main)") for i in range(6): print(".") sys.stdout.flush() time.sleep(0.1) print("ok") th.join() if update_version is not None: print(f"A new release of gh is available: xxx → {update_version}")
async def run(): fut = asyncio.ensure_future(update_check()) run_main() update_version = await fut if update_version is not None: print(f"A new release of gh is available: xxx → {update_version}")
def foo(): print("hello") print("byebye")