Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork12.1k
Closed
Description
While it is threadsafe, thewith statement gives the false impression that it's always safe. It is not:
importasyncioimportnumpyasnpdefdivide_by_zero():np.float64(1)/0asyncdeffoo():forjinrange(3):withnp.errstate(divide='raise'):foriinrange(2):try:divide_by_zero()exceptFloatingPointError:passelse:print("foo: Failed to raise!")awaitasyncio.sleep(0.15)asyncdefbar():forjinrange(3):withnp.errstate(divide='ignore'):foriinrange(2):try:divide_by_zero()exceptFloatingPointError:print("bar: raised anyway!")awaitasyncio.sleep(0.11)loop=asyncio.get_event_loop()loop.run_until_complete(asyncio.gather(foo(),bar()))loop.close()
gives:
foo: Failed to raise!bar: raised anyway!foo: Failed to raise!because the error-state is thread-local, but asyncio tasks share a thread.
I don't know if there is any way this can be fixed - it seems like this might be a python bug, and context managers might need an__yield_enter__ and__yield_leave__ mechanism, to be notified when control is leaving the suite via ayield/await