[Tutor] What exactly does await do? (ThreeBlindQuarks)(David)

Alphonsus phonokoye at gmail.com
Sun Feb 19 06:20:24 EST 2023


I am sorry that the question seemed overly complicated, here I will
try to make things as simple as things are. In reply to David's
question, I am actually writing a gui program but it is expected to do
a ton of request and on request by the user read a file, but while the
file is being read, I do not expect the program gui loop to stop, and
this is the same with the request.

I guess part of the problem is that I do not know where to keep the
asynchronous routine that fetches resources from the net, in relation
to the code and another problem for me is that while trying to
understand this from the docs, I am seeing a lot of terms that I do
not understand.

Normally I read a file using something like this:
with open(filename, 'r') as file_object:
    file_object.read()

but it is my experience that this will stop the gui loop temporarily
and this becomes noticeable if the file is large. Looking at the
python docs, there are three candidates to solve this problem,
asyncio, threading or multiprocessing, but I don't understand them
well enough to decide what's best and that is what I am trying to
understand.

I should also add that I found aiofiles package for reading to files
asynchronously and I can do that seperately but I still don't
understand much of what is going on behind the scenes to know where to
place any code anywhere. So my idea was to get information on these
modules and what is actually happening within them to help me write
good code and one that I can actually debug.

There are still things I do not understand but in trying to keep
things simple, I will ask only one question at a time and only if I
have understood and still find it necessary do I proceed with another
question?

Using aiofiles is probably the best option for reading asynchronously
from a file if you are a professional and understand what you are
doing, but as a beginner, I suggest that it will be better for me to
know how to read files asynchronously using the standard library
first, so that when I am using aiofiles I actually have an idea of
what aiofiles is doing in the background.

So my question is how do I read files asynchronously in python using
only what the standard library provides like io and asyncio modules,
but I am not asking for code (if you do provide one though, I don't
mind), I am looking for information of the objects that will appear in
the code, what the object will be doing, the functions that will be
needed in the code, what the functions will be doing, the loop and
what will be happening each time through the loop. Just general
information from which I can research further on. I just wish to
understand the concepts, not burden anyone with my tasks.


More information about the Tutor mailing list