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

Dennis Lee Bieber wlfraed at ix.netcom.com
Mon Feb 20 17:02:30 EST 2023


On Sun, 19 Feb 2023 12:20:24 +0100, Alphonsus <phonokoye at gmail.com>
declaimed the following:

>
>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 suspect multiprocessing is NOT an option here -- you are adding the
overhead of OS-level process creation, along with setting up a
communication protocol to transfer data between the processes.

	As for the GUI event loop... While the event dispatching may appear
asynchronous, many GUI system event loops are, at heart, sequential logic. 

<pseudo-code>
	while True:
		get next event from system queue
		look up registered event handler
		CALL event handler
</pseudo-code>

	I'd recommend studying the GUI toolkit for how /it/ recommends handling
long running events.

	Long running operations need to be either set up as, say, a separate
thread (I don't do asyncio -- I find threading a logical programming model
for the way I think... and my exposure to co-routines was what could be
implemented in FORTRAN-IV using ASSIGNed GOTO, or schemes where the
relinquishing routine specified which routine was to run next); or be
capable of being broken up into small chunks that can be run via a series
of invocations, using something like an On-Idle GUI event handler.

	Your example is opening a file, and reading ALL of it in one chunk (and
throwing the data away <G>). Instead I'd break the open/close (NO "with"
statement) out from the reading, and read in chunks (for a text file, read
one line, for binary, read some predefined length) -- with the reading
operation handled by an On-Idle handler. ("datastore" is presumed to be
some mutable/empty object used to collect the chunks from each small read)

<pseudo-code GUI initiation event>
	fhandle = open(...)
	schedule On-Idle handler (reader, fhandle, datastore)
</pseudo-code>
<pseudo-code reader>
	data = fhandle.read_line
	if EOF: 
		fhandle.close
		signal GUI that data is complete
	else:
		datastore.append(data)
</pseudo-code>

	A threaded version would have the initiation event create&start the
reader thread, passing the file name and other parameters. The thread can
use the "with" statement form to read the file, and would end with
signalling the GUI that the data is complete. For tkinter, look at:
https://pythonassets.com/posts/background-tasks-with-tk-tkinter/

	The On-Idle example can probably be reworked using a generator function
("yield" statement).

>
>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.

	The answer may vary depending on the rest of the code... If you are
using a GUI toolkit, you first need to know what features that toolkit
provides for "background processing" (the linked tkinter example relies
upon a standard Python thread for the read/write and uses one-shot timer
events [via .after()] to schedule a GUI event that checks for completion of
the reader thread; wxPython examples
https://wiki.wxpython.org/LongRunningTasks using thread, wxPython wxYield,
and an OnIdle event). Note that neither example mixes async co-routines
with sequential dispatch logic.

	asyncio/await (to me) just doesn't fit the concept of a long-running
process (reading/writing a large file in one chunk). Note that await
serializes processing -- the function with the await statement stops at
that point until the waited-for function returns; so using await in a GUI
event handler to wait for the read/write operation to complete is going to
block the GUI.


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
	wlfraed at ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/



More information about the Tutor mailing list