Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 - http://pymotw.com/2/signal/#signals-and-threads 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!

like image 956
Vikraant Singh Sanwal Avatar asked Aug 06 '14 08:08

Vikraant Singh Sanwal


People also ask

Is thread join necessary in Python?

Need to Join a Thread Every Python program has at least one thread of execution called the main thread. Both processes and threads are created and managed by the underlying operating system. Sometimes we may need to create additional threads in our program in order to execute code concurrently.

What happens if you don't join a thread Python?

A Python thread is just a regular OS thread. If you don't join it, it still keeps running concurrently with the current thread. It will eventually die, when the target function completes or raises an exception.

How do you send a message between threads in Python?

Perhaps the safest way to send data from one thread to another is to use a Queue from the queue library. To do this, create a Queue instance that is shared by the threads. Threads then use put() or get() operations to add or remove items from the queue as shown in the code given below.

Is thread join necessary?

It is perfectly fine. No join or System. exit necessary. Each thread lives its own life.


1 Answers

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.

source

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()

output

Waiting for event
event set.
like image 126
johntellsall Avatar answered Sep 28 '22 06:09

johntellsall