Asynchronous programming

Christian Gollwitzer auriocus at gmx.de
Thu Aug 11 02:21:36 EDT 2016


Am 11.08.16 um 05:53 schrieb Steven D'Aprano:
> How is this the same as, or different from, event-based programming? I'm
> familiar with programming in an event-based language where the interpreter
> itself provides an event loop and dispatches messages to your objects

I'll just comment on  that one. Asynchronous programming is event based 
programming. In fact if you drop down to the C level, it works the same 
- you get callbacks when new data arives over a socket, or when a timer 
triggers. asyncio is just a way that makes writing stateful callback 
handlers easier, in the same way that generators are easier to write 
using "yield" than as a function with a global variable.

In typical GUI code, there are usually not that many places qhere ou 
have sequential code. A simple exmaple might be a counter. Using asyncio 
and a properly integrated GUI toolkit, you could write it as (pseudo-code)

async def counter():
	for i in range(10):
		button.settext("click me %d"%i)
		await button.click()
	
	button.disable()
	messageBox("You reached the end!")

Writing that same thing in callback code to button.on_click() is 
obviously less fun and feels inverted.

	Christian



More information about the Python-list mailing list