How to send a signal to the main thread in python without using join?


I Am trying to send a signal from a child thread to the main thread in a multi-threaded program (cannot use multi-processes). Unfortunately even after exhausting all the reading materials available online (which I could find), I Am unable to get a clear idea of how to do so. I Am a beginner to signals AND to python so please bear with me and explain as you would to a novice. I cannot use the join method in the process, since I want both the threads to be running simultaneously. Here is the code that I found related to the topic here - <a href="http://pymotw.com/2/signal/#signals-and-threads" rel="nofollow">http://pymotw.com/2/signal/#signals-and-threads</a> and it doesn't really work for me.

import signal import threading import os import time def signal_handler(num, stack): print 'Received signal %d in %s' % (num, threading.currentThread()) signal.signal(signal.SIGUSR1, signal_handler) def wait_for_signal(): print 'Waiting for signal in', threading.currentThread() signal.pause() print 'Done waiting' # Start a thread that will not receive the signal receiver = threading.Thread(target=wait_for_signal, name='receiver') receiver.start() time.sleep(0.1) def send_signal(): print 'Sending signal in', threading.currentThread() os.kill(os.getpid(), signal.SIGUSR1) sender = threading.Thread(target=send_signal, name='sender') sender.start() sender.join() # Wait for the thread to see the signal (not going to happen!) print 'Waiting for', receiver signal.alarm(2) receiver.join()

Please explain with a multi-threaded example if possible. Thanks in advance!


Signals and threads really, really don't play nice together.

Consider use an Event or other synchronization mechanism. The following example creates an 'event' object, then passes it to two threads. One waits for two seconds, then signals the other to print out a message then exit.

<h2>source</h2> import threading, time def flagger_thread(event): """ wait for two seconds, then make 'event' fire """ time.sleep(2) event.set() def waiter_thread(event): print("Waiting for event") if event.wait(5): print("event set.") else: print("Timed out.") stop_event = threading.Event() threading.Thread(target=flagger_thread, args=[stop_event]).start() threading.Thread(target=waiter_thread, args=[stop_event]).start() # wait for all threads to exit for t in threading.enumerate(): if t != threading.current_thread(): t.join() <h2>output</h2> Waiting for event event set.


  • Sort os.listdir files Python
  • asp.net MVC ActionFilter for remove empty lines in result
  • Gradle distZip config files
  • Why is SQLite refusing to use available indexes when adding a JOIN?
  • Separate number from unit in a string in Python
  • Take photo using webcam is giving black output[Unity3D]
  • XMPP Bosh vs SignalR
  • Take next parameter as field width in String.Format
  • How to provide a default typeclass for generic types in Scala?
  • Jackson complex list serialization
  • How to serialize Xml Date only from DateTime in C#
  • Add Stylesheet reference to XML Document in Linq?
  • grails date format in English language
  • JTable setValueAt StackOverflowError
  • pymssql ( python module ) unable to use temporary tables
  • Generalizing from a specific type to a class in a GADT
  • HTML5 Video Play/Pause issue
  • How to know when bluetooth disconnected
  • Python Django ImportError: No module named website
  • R Reference Class multiple inheritance: how to call method in a specific parent class?
  • Ruby on Rails - Settting up Reviews functionality
  • Spongycastle is missing many algorithms when loaded on android
  • Shrinking Bootstrap Navbar with logo on scroll
  • Adding native code to an existing Worklight hybrid app
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • time column in sqlite using gorm
  • How to create subsets of a single set of elements with XSLT?
  • When to use the tag in the head and body section of a html page? [duplicate]
  • how to run a different select statement based on condition in Hive SQL
  • Creating 2d platforms using JavaScript
  • How to write seo friendly url's using htaccess?
  • How to split wav file into two or more parts using c#
  • gnuplot - How to make zmin equal to zmax keeeping autoscale on z axis
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Content-Type alternative in MQTT
  • Conflicting declaration using constexpr and auto in C++11
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Call Microservice from another Microservice within Docker
  • Using Service Component Runtime
  • How to check if object is null in Java?