here's a reproducer:
import gc
import tornado.web
import tornado.httpclient
import asyncio
import contextlib
def make_handler(cb):
class MainHandler(tornado.web.RequestHandler):
async def get(self):
cb()
self.write("hello")
await asyncio.Event().wait()
return MainHandler
async def run_webserver(cb):
application = tornado.web.Application([(r"/", make_handler(cb))])
server = application.listen(8888)
try:
await asyncio.Event().wait()
finally:
await server.close_all_connections()
async def make_request():
with contextlib.closing(
tornado.httpclient.AsyncHTTPClient(force_instance=True)
) as client:
await client.fetch("http://localhost:8888/")
async def garbage_collect_on_request(event, done):
await event.wait()
gc.collect()
done()
async def main():
event = asyncio.Event()
class Result(Exception):
pass
def done():
raise Result()
try:
await asyncio.gather(
run_webserver(event.set),
make_request(),
garbage_collect_on_request(event, done),
)
except Result:
pass
asyncio.run(main())
note the note on https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task
Important: Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable “fire-and-forget” background tasks, gather them in a collection
avoid calls to make_current() and make_clear() by using asyncio.run in LoopRunner
Closes #6784
Various cleanups in semaphore (#5885)
avoid calls to make_current() and make_clear() by using asyncio.run in LoopRunner
Closes #6784
Set lifetime-stagger
default value to None
(#7445)
avoid calls to make_current() and make_clear() by using asyncio.run in LoopRunner
Closes #6784
avoid calls to make_current() and make_clear() by using asyncio.run in LoopRunner
Closes #6784
avoid calls to make_current() and make_clear() by using asyncio.run in LoopRunner
Closes #6784
Closes #6784
pre-commit run --all-files
avoid calls to make_current() and make_clear() by using asyncio.run in LoopRunner
Closes #6784
Zooming back a bit, why is this change needed? From skimming the PEP, it looks like it's for improved Windows support. Is that the case? Are there other reasons?
There's an unfortunate bit of noise in the tests, but the main advantage is being able to load the dask config file containing special characters on a platform with encoding not set to UTF-8 (eg some docker containers), and detecting new code that is introduced that is not explicit about the encoding because the python default behaviour is set to change in the future.
yaml and json both support auto-detecting the encoding of the passed in file, but only if the file is opened in bytes mode - opening the file in text mode bypasses that feature.
regarding the SRI files omitting the encoding kwarg may be a false positive of PEP 597 it still throws the warning on import for downstream projects.
I could have passed encoding="ascii"
here, but opening the the json file in bytes mode also supports auto-detecting the encoding so I opted for that
yeah that's the reference I had in mind:
when you open a yaml file with rb
yaml can auto-detect the encoding:
>>> with open("example.yaml", "w", encoding="utf-16") as f:
... yaml.dump({}, f)
...
>>> with open("example.yaml", "rb") as f:
... yaml.safe_load(f)
...
{}
but if you use the "locale" encoding the determine_encoding feature doesn't work:
>>> with open("example.yaml") as f:
... yaml.safe_load(f)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/yaml/__init__.py", line 125, in safe_load
return load(stream, SafeLoader)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/yaml/__init__.py", line 79, in load
loader = Loader(stream)
^^^^^^^^^^^^^^
File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/yaml/loader.py", line 34, in __init__
Reader.__init__(self, stream)
File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/yaml/reader.py", line 85, in __init__
self.determine_encoding()
File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/yaml/reader.py", line 124, in determine_encoding
self.update_raw()
File "/home/graingert/.virtualenvs/testing311/lib/python3.11/site-packages/yaml/reader.py", line 178, in update_raw
data = self.stream.read(size)
^^^^^^^^^^^^^^^^^^^^^^
File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte