The Event
class is a very simple extension of a Condition
along with an additional Semaphore
. A Condition
contains a Lock
and a couple more semaphores, and a Lock is just a Semaphore
with a maximum value of only 1, so the question can really be boiled down to how is a Semaphore
passed to another process...
When creating a process using a Spawning context or when sending a lock over a queue or pipe, it is serialized using pickle
:
The base class for both Semaphore
and Lock
is SemLock
, which defines the __getstate__
and __setstate__
methods in order to make it able to be sent across a pipe using the pickle
protocol.
On the sending side, __getstate__
will obtain a handle to the lock which can be sent. On Windows one cannot simply take the handle returned by CreateSemaphore
and use it in another process, so it is instead first duplicated with DuplicateHandle
giving it the target process where it will be used. On *nix the handle is simply copied.
On the receiving side, the handle number is used along with a unique name and some other attributes which are copied over in order to rebuild the semaphore. This is a pretty simple process of unpacking values into a new SemLockObject
struct, which contains the handle to the underlying OS level semaphore.
When creating a process using a Forking context
The new process gets a complete copy of the memory of the parent process, including the objects containing references to the semaphore handles (see: forking and process copy-on-write). As windows is not involved here, the simple copying of the handle value is valid.