I
Isaac To
Hung> This sounds like event-driven programming to me.
Hung> (1) Usage of threads in principle is optional, but at Python level
Hung> event-driven programming is probably always done in multi-threaded
Hung> environment.
That does not stop somebody who wants something cheaper than threads when he
know that at most one place of his program needs being serviced at each
time.
Hung> (2) Also, at software level, interruptions are always emulated by
Hung> polling (i.e. event loop.) You will have a loop somewhere.
At "software level", most event loops are waited by calling the OS. The OS
can deal with the hardware interrupts caused by devices or timers, or the
signalling caused by other processes. In these ways the OS will not need to
do polling most of the time.
Hung> (3) I won't get into details of the event-handler implementation,
Hung> as it is well-known and you probably already know it or can find
Hung> it easily. But I'd like to mention a simple implementation using
Hung> exception handling. For instance:
Hung> while 1: try: poll_keyboard() poll_mouse() poll_microphone()
Hung> poll_temperature_sensor() except IncomingSignal, e: ... handle
Hung> event ...
Hung> You raise the IncomingSignal event inside the various polling
Hung> routines.
Never do that! You have a lot of ways to avoid holding up the CPU without
doing anything real. No event loop in real systems acts like that, except
for embedded systems that has exactly one program loaded into the memory at
the same time. Instead, use something like select.poll() to wait from more
than one source of input/output, or use a thread as suggested by the parent
thread.
Regards,
Isaac.
Hung> (1) Usage of threads in principle is optional, but at Python level
Hung> event-driven programming is probably always done in multi-threaded
Hung> environment.
That does not stop somebody who wants something cheaper than threads when he
know that at most one place of his program needs being serviced at each
time.
Hung> (2) Also, at software level, interruptions are always emulated by
Hung> polling (i.e. event loop.) You will have a loop somewhere.
At "software level", most event loops are waited by calling the OS. The OS
can deal with the hardware interrupts caused by devices or timers, or the
signalling caused by other processes. In these ways the OS will not need to
do polling most of the time.
Hung> (3) I won't get into details of the event-handler implementation,
Hung> as it is well-known and you probably already know it or can find
Hung> it easily. But I'd like to mention a simple implementation using
Hung> exception handling. For instance:
Hung> while 1: try: poll_keyboard() poll_mouse() poll_microphone()
Hung> poll_temperature_sensor() except IncomingSignal, e: ... handle
Hung> event ...
Hung> You raise the IncomingSignal event inside the various polling
Hung> routines.
Never do that! You have a lot of ways to avoid holding up the CPU without
doing anything real. No event loop in real systems acts like that, except
for embedded systems that has exactly one program loaded into the memory at
the same time. Instead, use something like select.poll() to wait from more
than one source of input/output, or use a thread as suggested by the parent
thread.
Regards,
Isaac.