summaryrefslogtreecommitdiff
path: root/demo4/child.py
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2014-09-12 14:16:22 +0000
committer <>2014-10-24 11:04:40 +0000
commita77e3a63f004e6ee789fa05e4a5bbc333b1529f1 (patch)
treeefe8a68996c19b7b0765ebc689937721d1d99cf7 /demo4/child.py
downloadsysv-ipc-tarball-master.tar.gz
Imported from /home/lorry/working-area/delta_python-packages_sysv-ipc-tarball/sysv_ipc-0.6.8.tar.gz.HEADsysv_ipc-0.6.8master
Diffstat (limited to 'demo4/child.py')
-rw-r--r--demo4/child.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/demo4/child.py b/demo4/child.py
new file mode 100644
index 0000000..f635769
--- /dev/null
+++ b/demo4/child.py
@@ -0,0 +1,23 @@
+import sysv_ipc
+import time
+import sys
+import random
+
+# The parent passes the semaphore's name to me.
+key = sys.argv[1]
+
+sem = sysv_ipc.Semaphore(int(key))
+
+print('Child: waiting to aquire semaphore ' + key)
+
+with sem:
+ print('Child: semaphore {} aquired; holding for 3 seconds.'.format(sem.key))
+
+ # Flip a coin to determine whether or not to bail out of the context.
+ if random.randint(0, 1):
+ print("Child: raising ValueError to demonstrate unplanned context exit")
+ raise ValueError
+
+ time.sleep(3)
+
+ print('Child: gracefully exiting context (releasing the semaphore).')