From f3765db04b903b3671733e07cf1541a51966dd14 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Fri, 21 Feb 2014 04:46:49 +0000 Subject: Imported from /home/lorry/working-area/delta_python-packages_posix-ipc-tarball/posix_ipc-0.9.8.tar.gz. --- demo3/repeating_signal.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 demo3/repeating_signal.py (limited to 'demo3/repeating_signal.py') 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) + -- cgit v1.2.1