Open jrs65 opened 4 years ago
import logging
import asyncio
from asyncio import CancelledError, TimeoutError
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
WAIT_TIME = 2
lock_datasets = asyncio.Lock()
signal_datasets_updated = asyncio.Condition(lock_datasets)
async def notify(delay):
for i in range(4):
await asyncio.sleep(delay)
async with signal_datasets_updated:
signal_datasets_updated.notify_all()
async def wait_for_dset(id):
"""Wait until the given dataset is present."""
logger.debug("wait_for_ds: Waiting for dataset {}".format(id))
async with lock_datasets:
while True:
try:
await asyncio.wait_for(signal_datasets_updated.wait(), WAIT_TIME)
except (TimeoutError, CancelledError):
logger.warning(
"wait_for_ds: Timeout ({}s) when waiting for dataset {}".format(
WAIT_TIME, id
)
)
return False
if id == 0:
logger.debug("wait_for_ds: Found dataset {}".format(id))
break
logger.info("decreasing id")
id -= 1
if __name__ == "__main__":
import time
s = time.perf_counter()
loop.run_until_complete(asyncio.gather(wait_for_dset(3), notify(1)))
elapsed = time.perf_counter() - s
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
Occasionally comet will give this error:
This looks suspiciously similar to python/cpython#7216, however that should have been fixed in Python 3.7 which we are running. I can't easily reproduce this locally. I've attached an example script which is similar to the relevant code in coco which doesn't fail.
Also, things are a little weird with branches. The version running above in this failure seems to be d030e1c1634e