summaryrefslogtreecommitdiff
path: root/demo3
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
downloadposix-ipc-tarball-master.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')
-rw-r--r--demo3/ReadMe.txt12
-rwxr-xr-xdemo3/cleanup.py13
-rw-r--r--demo3/one_shot_signal.py45
-rw-r--r--demo3/one_shot_thread.py38
-rw-r--r--demo3/repeating_signal.py46
-rw-r--r--demo3/repeating_thread.py40
-rw-r--r--demo3/utils.py15
7 files changed, 209 insertions, 0 deletions
diff --git a/demo3/ReadMe.txt b/demo3/ReadMe.txt
new file mode 100644
index 0000000..16f1fc5
--- /dev/null
+++ b/demo3/ReadMe.txt
@@ -0,0 +1,12 @@
+These scripts demonstrate four message queue notification techniques.
+
+All of demos ask you to enter a message. That message is then sent to the
+queue and received in a notification handler and printed to stdout.
+
+- one_shot_signal.py and one_shot_thread.py receive their notifications via a
+signal and thread, respectively. After one message & notification, the demo
+exits.
+
+- repeating_signal.py and repeating_thread.py are similar, except that they
+re-register for notifications in their notification handler so you can
+enter as many messages as you like.
diff --git a/demo3/cleanup.py b/demo3/cleanup.py
new file mode 100755
index 0000000..53ada42
--- /dev/null
+++ b/demo3/cleanup.py
@@ -0,0 +1,13 @@
+import posix_ipc
+import utils
+
+try:
+ posix_ipc.unlink_message_queue(utils.QUEUE_NAME)
+ s = "message queue %s removed" % utils.QUEUE_NAME
+ print (s)
+except:
+ print ("queue doesn't need cleanup")
+
+
+
+print ("\nAll clean!")
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)
+
diff --git a/demo3/one_shot_thread.py b/demo3/one_shot_thread.py
new file mode 100644
index 0000000..c669370
--- /dev/null
+++ b/demo3/one_shot_thread.py
@@ -0,0 +1,38 @@
+# Python modules
+import time
+
+# 3rd party modules
+import posix_ipc
+
+# Utils for this demo
+import utils
+
+
+def process_notification(mq):
+ 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( (process_notification, mq) )
+
+# Get user input and send it to the queue.
+print ("Enter a message:")
+mq.send(utils.get_input())
+
+# The callback happens 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)
+
diff --git a/demo3/repeating_signal.py b/demo3/repeating_signal.py
new file mode 100644
index 0000000..2fed73f
--- /dev/null
+++ b/demo3/repeating_signal.py
@@ -0,0 +1,46 @@
+# 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))
+
+ # Re-register for notifications
+ mq.request_notification(MY_SIGNAL)
+
+
+# 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.
+msg = "42"
+while msg:
+ print ("\nEnter a message. A blank message will end the demo:")
+ msg = utils.get_input()
+ if msg:
+ mq.send(msg)
+
+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)
+
diff --git a/demo3/repeating_thread.py b/demo3/repeating_thread.py
new file mode 100644
index 0000000..e456428
--- /dev/null
+++ b/demo3/repeating_thread.py
@@ -0,0 +1,40 @@
+# Python modules
+import time
+
+# 3rd party modules
+import posix_ipc
+
+# Utils for this demo
+import utils
+
+
+def process_notification(mq):
+ message, priority = mq.receive()
+
+ print ("Ding! Message with priority %d received: %s" % (priority, message))
+
+ # Re-register for notifications
+ mq.request_notification( (process_notification, mq) )
+
+
+
+# Create the message queue.
+mq = posix_ipc.MessageQueue(utils.QUEUE_NAME, posix_ipc.O_CREX)
+
+# Request notifications
+mq.request_notification( (process_notification, mq) )
+
+# Get user input and send it to the queue.
+s = "42"
+while s:
+ print ("\nEnter a message. A blank message will end the demo:")
+ s = utils.get_input()
+ if s:
+ mq.send(s)
+
+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)
+
diff --git a/demo3/utils.py b/demo3/utils.py
new file mode 100644
index 0000000..67e066f
--- /dev/null
+++ b/demo3/utils.py
@@ -0,0 +1,15 @@
+# Python modules
+import sys
+
+
+QUEUE_NAME = "/my_message_queue"
+
+
+PY_MAJOR_VERSION = sys.version_info[0]
+
+def get_input():
+ """Get input from user, Python 2.x and 3.x compatible"""
+ if PY_MAJOR_VERSION > 2:
+ return input()
+ else:
+ return raw_input()