2022-01-11 16:55:24 +00:00
|
|
|
"""Block blocking calls being done in asyncio."""
|
2020-04-15 22:32:10 +00:00
|
|
|
from http.client import HTTPConnection
|
2022-01-11 16:55:24 +00:00
|
|
|
import time
|
2020-04-15 22:32:10 +00:00
|
|
|
|
2021-12-23 19:14:47 +00:00
|
|
|
from .util.async_ import protect_loop
|
2020-04-15 22:32:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def enable() -> None:
|
2022-01-11 16:55:24 +00:00
|
|
|
"""Enable the detection of blocking calls in the event loop."""
|
2020-04-15 22:32:10 +00:00
|
|
|
# Prevent urllib3 and requests doing I/O in event loop
|
2023-03-08 21:57:54 +00:00
|
|
|
HTTPConnection.putrequest = protect_loop( # type: ignore[method-assign]
|
2023-01-15 22:00:51 +00:00
|
|
|
HTTPConnection.putrequest
|
|
|
|
)
|
2020-04-15 22:32:10 +00:00
|
|
|
|
2022-01-11 16:55:24 +00:00
|
|
|
# Prevent sleeping in event loop. Non-strict since 2022.02
|
|
|
|
time.sleep = protect_loop(time.sleep, strict=False)
|
|
|
|
|
2020-04-15 22:32:10 +00:00
|
|
|
# Currently disabled. pytz doing I/O when getting timezone.
|
|
|
|
# Prevent files being opened inside the event loop
|
|
|
|
# builtins.open = protect_loop(builtins.open)
|