diff options
author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2014-02-21 04:46:49 +0000 |
---|---|---|
committer | <> | 2014-08-04 21:38:17 +0000 |
commit | f3765db04b903b3671733e07cf1541a51966dd14 (patch) | |
tree | defcc3c47d9b8bd78b97dcc04ee779a758d37b1c /demo4/parent.py | |
download | posix-ipc-tarball-f3765db04b903b3671733e07cf1541a51966dd14.tar.gz |
Imported from /home/lorry/working-area/delta_python-packages_posix-ipc-tarball/posix_ipc-0.9.8.tar.gz.HEADposix_ipc-0.9.8master
Diffstat (limited to 'demo4/parent.py')
-rw-r--r-- | demo4/parent.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/demo4/parent.py b/demo4/parent.py new file mode 100644 index 0000000..799a6e7 --- /dev/null +++ b/demo4/parent.py @@ -0,0 +1,46 @@ +import subprocess +import posix_ipc +import time +import os + +sem = posix_ipc.Semaphore(None, posix_ipc.O_CREX, initial_value = 1) +print("Parent: created semaphore {}.".format(sem.name)) + +sem.acquire() + +# Spawn a child that will wait on this semaphore. +path, _ = os.path.split(__file__) +print("Parent: spawning child process...") +subprocess.Popen(["python", os.path.join(path, 'child.py'), sem.name]) + +for i in range(3, 0, -1): + print("Parent: child process will acquire the semaphore in {} seconds...".format(i)) + time.sleep(1) + +sem.release() + +# Sleep for a second to give the child a chance to acquire the semaphore. +# This technique is a little sloppy because technically the child could still +# starve, but it's certainly sufficient for this demo. +time.sleep(1) + +# Wait for the child to release the semaphore. +print("Parent: waiting for the child to release the semaphore.") +sem.acquire() + +# Clean up. +print("Parent: destroying the semaphore.") +sem.release() +sem.unlink() + +msg = """ +By the time you're done reading this, the parent will have exited and so the +operating system will have destroyed the semaphore. You can prove that the +semaphore is gone by running this command and observing that it raises +posix_ipc.ExistentialError -- + + python -c "import posix_ipc; posix_ipc.Semaphore('{}')" + +""".format(sem.name) + +print(msg) |