summaryrefslogtreecommitdiff
path: root/demo3/one_shot_signal.py
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2014-02-21 04:46:49 +0000
committer <>2014-08-04 21:38:17 +0000
commitf3765db04b903b3671733e07cf1541a51966dd14 (patch)
treedefcc3c47d9b8bd78b97dcc04ee779a758d37b1c /demo3/one_shot_signal.py
downloadposix-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 'demo3/one_shot_signal.py')
-rw-r--r--demo3/one_shot_signal.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/demo3/one_shot_signal.py b/demo3/one_shot_signal.py
new file mode 100644
index 0000000..23efe08
--- /dev/null
+++ b/demo3/one_shot_signal.py
@@ -0,0 +1,45 @@
+# Python modules
+import time
+import signal
+
+# 3rd party modules
+import posix_ipc
+
+# Utils for this demo
+import utils
+
+
+MY_SIGNAL = signal.SIGUSR1
+
+
+def handle_signal(signal_number, stack_frame):
+ message, priority = mq.receive()
+
+ print ("Ding! Message with priority %d received: %s" % (priority, message))
+
+
+
+# Create the message queue.
+mq = posix_ipc.MessageQueue(utils.QUEUE_NAME, posix_ipc.O_CREX)
+
+# Request notifications
+mq.request_notification(MY_SIGNAL)
+
+# Register my signal handler
+signal.signal(MY_SIGNAL, handle_signal)
+
+# Get user input and send it to the queue.
+print ("Enter a message:")
+mq.send(utils.get_input())
+
+# The signal fires almost instantly, but if I don't pause at least
+# briefly then the main thread may exit before the notification fires.
+print ("Sleeping for one second to allow the notification to happen.")
+time.sleep(1)
+
+print ("Destroying the message queue.")
+mq.close()
+# I could call simply mq.unlink() here but in order to demonstrate
+# unlinking at the module level I'll do it that way.
+posix_ipc.unlink_message_queue(utils.QUEUE_NAME)
+