Windows has event objects which can be created and fired system-wide. This is useful for signalling between processes. Eg each of three cooperating processes could create the same event and then use a thread (cf event_handler below) which would block on the event to perform some useful action, for example a data reload. Python also has an event construct, based around the threading support module. Although it is intra-process only, its basic functionality is the same as the Windows events (set, reset, wait).
This module implements a common event interface, and offers a python version of it (PythonEvent) and a Windows version (Win32Event) where available. The module behaves perfectly well on Linux simply by not creating the Win32Event class. The original intention of the module was to wrap the Win32 event; the Python version was something of an afterthought. If I had time I could probably use the signal module to simulate, under Linux, the inter-process behaviour present in the Windows events.
python setup.py installas usual.
SHARED_MEMORY_NAME = "shared_memory" SHARED_MEMORY_SIZE = 16 * 1024 READY_TO_CONSUME = "ready_to_consume" FINISHED_PRODUCING = "finished_producing"
import mmap import events import common shared_memory = mmap.mmap (0, common.SHARED_MEMORY_SIZE, common.SHARED_MEMORY_NAME) ready_to_consume_event = events.Win32Event (common.READY_TO_CONSUME) finished_producing_event = events.Win32Event (common.FINISHED_PRODUCING) line_number = 0 while 1: ready_to_consume_event.wait () shared_memory.write ("This is line %d\n" % line_number) line_number += 1 finished_producing_event.set ()
import mmap import time import events import common shared_memory = mmap.mmap (0, common.SHARED_MEMORY_SIZE, common.SHARED_MEMORY_NAME) ready_to_consume_event = events.Win32Event (common.READY_TO_CONSUME) finished_producing_event = events.Win32Event (common.FINISHED_PRODUCING) while 1: ready_to_consume_event.set () finished_producing_event.wait () print shared_memory.readline () time.sleep (2)
25th November 2003: events.py (v0.1, zipped - tiny)1±È1Õæ½ð²¶Óã