> It opens the file in the main thread, and not asynchronously, but doesn't
the file itself get read in the other thead, asynchronously? And is there
any extra RAM used?

The file could be on an external network drive and so opening it may block the 
main thread for seconds:

```
with open(the_path, 'rb') as the_file:  # no other tasks can progress while 
opening this file
    return await asyncio.to_thread(json.load, the_file)
```

> Also -- and this may be completely my ignorance -- but don't you need to
wrap the json reading in a function anyway so that you can capture the
result somewhere?

asyncio.to_thread is an async function that returns the result of the passed in 
synchronous function, and can run concurrently with other tasks by scheduling 
the synchronous function on a thread



> In short -- no, I don't think JSON is not special enough to get a Path
method, but a simple way to read JSON directly from a Path would be nice.

```
asyncio.to_thread(json.loadf, the_path)
```
would be fine too
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/WVA2E4RPLTK5N3NGVT66H4HDH3MURXWC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to