Hướng dẫn python wait for signal - trăn chờ tín hiệu

Signal

UNIX/Linux systems cung cấp các cơ chế đặc biệt để communicate giữa các process. Một trong số đó lấy tên là signals.

Một cách ngắn gọn, a signal giống như một notification của một event. Khi một event nào đó xảy ra trong system, một signal sẽ được tạo ra để notify tới các chương trình khác về event này.

Một ví dụ đơn giản: Khi bạn đang run một command trên terminal. Câu lệnh đang chạy, mà bạn lại sử dụng tổ hợp phím ctrl + C . Khi đó, một signal được gọi là

Sending a signal
    The following system calls and library functions allow the caller to send a signal:

    raise(3)        Sends a signal to the calling thread.

    kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.

    killpg(2)       Sends a signal to all of the members of a specified process group.

    pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.

    tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)

    sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught
    The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):

    pause(2)        Suspends execution until any signal is caught.

    sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal
    Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
    ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:

    * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
        the delivered signal.

    * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
        until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
        nal.

0 sinh ra. Và chương trình terminal sẽ đọc tín hiệu đó và thực thi việc dừng command lại.

Trên UNIX-based systems, sẽ có 3 loại signals chính:

  • System signals:
    • SIGILL
    • SIGTRAP
    • SIGBUS
    • SIGFPE,
    • SIGKILL
    • SIGSEGV
    • SIGXCPU
    • SIGXFSZ
    • SIGIO
  • Device signals:
    • SIGHUP
    • SIGINT
    • SIGPIPE
    • SIGALRM
    • SIGCHLD
    • SIGCONT
    • SIGSTOP
    • SIGTTIN
    • SIGTTOU
    • SIGURG
    • SIGWINCH
    • SIGIO
  • Device signals:
    • SIGHUP
    • SIGINT
    • SIGUSR1
    • SIGUSR2
    • SIGPIPE

SIGALRM

SIGCHLD

man 7 signal
....
First the signals described in the original POSIX.1-1990 standard.

Signal     Value     Action   Comment
──────────────────────────────────────────────────────────────────────
SIGHUP        1       Term    Hangup detected on controlling terminal or death of controlling process
SIGINT        2       Term    Interrupt from keyboard
SIGQUIT       3       Core    Quit from keyboard
SIGILL        4       Core    Illegal Instruction
SIGABRT       6       Core    Abort signal from abort(3)
SIGFPE        8       Core    Floating point exception
SIGKILL       9       Term    Kill signal
SIGSEGV      11       Core    Invalid memory reference
SIGPIPE      13       Term    Broken pipe: write to pipe with no
                                readers
SIGALRM      14       Term    Timer signal from alarm(2)
SIGTERM      15       Term    Termination signal
SIGUSR1   30,10,16    Term    User-defined signal 1
SIGUSR2   31,12,17    Term    User-defined signal 2
SIGCHLD   20,17,18    Ign     Child stopped or terminated
SIGCONT   19,18,25    Cont    Continue if stopped
SIGSTOP   17,19,23    Stop    Stop process
SIGTSTP   18,20,24    Stop    Stop typed at terminal
SIGTTIN   21,21,26    Stop    Terminal input for background process
SIGTTOU   22,22,27    Stop    Terminal output for background process

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
....

SIGCONT

Sending a signal
    The following system calls and library functions allow the caller to send a signal:

    raise(3)        Sends a signal to the calling thread.

    kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.

    killpg(2)       Sends a signal to all of the members of a specified process group.

    pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.

    tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)

    sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught
    The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):

    pause(2)        Suspends execution until any signal is caught.

    sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal
    Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
    ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:

    * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
        the delivered signal.

    * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
        until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
        nal.

SIGSTOP

SIGTTIN

SIGTTOU

SIGURG

import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 

SIGWINCH

User-defined signals:

SIGQUIT

SIGABRT

SIGTERM

% python test_signal.py                                                                                                                                     
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c

Mỗi một signal sẽ được đại diện bởi một integer value.

Trên Ubuntu, bạn có thể check list signal này :

Khi thực hiện lệnh

Sending a signal
    The following system calls and library functions allow the caller to send a signal:

    raise(3)        Sends a signal to the calling thread.

    kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.

    killpg(2)       Sends a signal to all of the members of a specified process group.

    pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.

    tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)

    sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught
    The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):

    pause(2)        Suspends execution until any signal is caught.

    sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal
    Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
    ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:

    * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
        the delivered signal.

    * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
        until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
        nal.

1, bạn cũng sẽ thể thấy cách sử dụng signal trong Ubuntu nó như thế nào:

Python signal

  • Từ bản Python 1.4,
    Sending a signal
        The following system calls and library functions allow the caller to send a signal:
    
        raise(3)        Sends a signal to the calling thread.
    
        kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.
    
        killpg(2)       Sends a signal to all of the members of a specified process group.
    
        pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.
    
        tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)
    
        sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.
    
    Waiting for a signal to be caught
        The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):
    
        pause(2)        Suspends execution until any signal is caught.
    
        sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.
    
    Synchronously accepting a signal
        Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
        ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:
    
        * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
            the delivered signal.
    
        * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
            until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
            nal.
    
    
    2 library đã được tích hợp và cập nhật thường xuyên vào trong core.
  • Python Basic
  • Một ví dụ nhỏ về sử dụng
    Sending a signal
        The following system calls and library functions allow the caller to send a signal:
    
        raise(3)        Sends a signal to the calling thread.
    
        kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.
    
        killpg(2)       Sends a signal to all of the members of a specified process group.
    
        pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.
    
        tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)
    
        sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.
    
    Waiting for a signal to be caught
        The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):
    
        pause(2)        Suspends execution until any signal is caught.
    
        sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.
    
    Synchronously accepting a signal
        Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
        ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:
    
        * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
            the delivered signal.
    
        * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
            until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
            nal.
    
    
    0 signal:
  • Line 1, 2 sẽ import lib
    Sending a signal
        The following system calls and library functions allow the caller to send a signal:
    
        raise(3)        Sends a signal to the calling thread.
    
        kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.
    
        killpg(2)       Sends a signal to all of the members of a specified process group.
    
        pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.
    
        tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)
    
        sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.
    
    Waiting for a signal to be caught
        The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):
    
        pause(2)        Suspends execution until any signal is caught.
    
        sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.
    
    Synchronously accepting a signal
        Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
        ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:
    
        * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
            the delivered signal.
    
        * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
            until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
            nal.
    
    
    2 và lib
    Sending a signal
        The following system calls and library functions allow the caller to send a signal:
    
        raise(3)        Sends a signal to the calling thread.
    
        kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.
    
        killpg(2)       Sends a signal to all of the members of a specified process group.
    
        pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.
    
        tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)
    
        sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.
    
    Waiting for a signal to be caught
        The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):
    
        pause(2)        Suspends execution until any signal is caught.
    
        sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.
    
    Synchronously accepting a signal
        Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
        ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:
    
        * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
            the delivered signal.
    
        * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
            until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
            nal.
    
    
    5.

Line 5, 6 define function handle signal. Trong function này mình sẽ print ra value integer của signal và frame mà nó nhận được cùng với signal.

Signal.connect(receiver, sender=None, weak=True, dispatch_uid=None)

Line 8 sử dụng

Sending a signal
    The following system calls and library functions allow the caller to send a signal:

    raise(3)        Sends a signal to the calling thread.

    kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.

    killpg(2)       Sends a signal to all of the members of a specified process group.

    pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.

    tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)

    sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught
    The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):

    pause(2)        Suspends execution until any signal is caught.

    sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal
    Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
    ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:

    * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
        the delivered signal.

    * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
        until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
        nal.

6 function để assign handle signal
Sending a signal
    The following system calls and library functions allow the caller to send a signal:

    raise(3)        Sends a signal to the calling thread.

    kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.

    killpg(2)       Sends a signal to all of the members of a specified process group.

    pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.

    tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)

    sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught
    The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):

    pause(2)        Suspends execution until any signal is caught.

    sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal
    Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
    ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:

    * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
        the delivered signal.

    * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
        until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
        nal.

0. Mỗi một khoảng thời gian, CPU nhận được
Sending a signal
    The following system calls and library functions allow the caller to send a signal:

    raise(3)        Sends a signal to the calling thread.

    kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.

    killpg(2)       Sends a signal to all of the members of a specified process group.

    pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.

    tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)

    sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught
    The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):

    pause(2)        Suspends execution until any signal is caught.

    sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal
    Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
    ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:

    * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
        the delivered signal.

    * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
        until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
        nal.

8, function
Sending a signal
    The following system calls and library functions allow the caller to send a signal:

    raise(3)        Sends a signal to the calling thread.

    kill(2)         Sends a signal to a specified process, to all members of a specified process group, or to all processes on the system.

    killpg(2)       Sends a signal to all of the members of a specified process group.

    pthread_kill(3) Sends a signal to a specified POSIX thread in the same process as the caller.

    tgkill(2)       Sends a signal to a specified thread within a specific process.  (This is the system call used to implement pthread_kill(3).)

    sigqueue(3)     Sends a real-time signal with accompanying data to a specified process.

Waiting for a signal to be caught
    The following system calls suspend execution of the calling process or thread until a signal is caught (or an unhandled signal terminates the process):

    pause(2)        Suspends execution until any signal is caught.

    sigsuspend(2)   Temporarily changes the signal mask (see below) and suspends execution until one of the unmasked signals is caught.

Synchronously accepting a signal
    Rather than asynchronously catching a signal via a signal handler, it is possible to synchronously accept the signal, that is, to block execution until the signal  is  deliv‐
    ered, at which point the kernel returns information about the signal to the caller.  There are two general ways to do this:

    * sigwaitinfo(2),  sigtimedwait(2),  and sigwait(3) suspend execution until one of the signals in a specified set is delivered.  Each of these calls returns information about
        the delivered signal.

    * signalfd(2) returns a file descriptor that can be used to read information about signals that are delivered to the caller.  Each read(2) from this  file  descriptor  blocks
        until  one  of the signals in the set specified in the signalfd(2) call is delivered to the caller.  The buffer returned by read(2) contains a structure describing the sig‐
        nal.

9 lại được thực hiện.

Line 10, 11, 12 sử dụng import signal import time def handler(a, b): print("Signal Number:", a, " Frame: ", b) signal.signal(signal.SIGINT, handler) while True: print("Press ctrl + c") time.sleep(10) 0 để chương trình luôn chạy.

Lưu đoạn code trên mà test thử :

def my_callback(sender, **kwargs):
    print("Request finished!")

Trên đây là ví dụ rất đơn giản về sử dụng sinal trong Python basic.

Django

Django cung cấp các

import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
1. Nó cho phép các app tách rời được notified khi các action xảy ra ở nơi khác trong framework.

  • Các signal built-in trong Django:
from django.core.signals import request_finished

request_finished.connect(my_callback)
  • import signal  
    import time  
      
      
    def handler(a, b): 
        print("Signal Number:", a, " Frame: ", b)  
      
    signal.signal(signal.SIGINT, handler) 
      
    while True:  
        print("Press ctrl + c")
        time.sleep(10) 
    
    2: Send trước hoặc sau khi save() method được thực hiện trong model.
from django.core.signals import request_finished
from django.dispatch import receiver

@receiver(request_finished)
def my_callback(sender, **kwargs):
    print("Request finished!")

import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
3: Send trước hoặc sau khi delete() method được thực hiện trong model.

import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
4: Sent khi
import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
5 on a model is changed.

import signal import time def handler(a, b): print("Signal Number:", a, " Frame: ", b) signal.signal(signal.SIGINT, handler) while True: print("Press ctrl + c") time.sleep(10) 6: Sent khi Django starts or finishes một HTTP request.

Trong django, sử dụng

import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
7 method để đăng ký một
import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
8 function.
import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
8 function này được called khi một signal được sent. Toàn bộ các signal’s receiver functions được gọi ở cùng một thời điểm và dựa theo thứ tự đăng ký.

Bài toán dưới đây là thực hiện in ra dòng chữ

% python test_signal.py                                                                                                                                     
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
0 trên console sau mõi request được hoàn thành.

from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import User


@receiver(post_save, sender=User)
def my_handler(sender, **kwargs):
    ...
    # Todo: Send email

Receiver functions

Đầu tiên,

import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
8 được define như thế nào ?

Lưu ý, trong hàm

import signal  
import time  
  
  
def handler(a, b): 
    print("Signal Number:", a, " Frame: ", b)  
  
signal.signal(signal.SIGINT, handler) 
  
while True:  
    print("Press ctrl + c")
    time.sleep(10) 
8 phải có
% python test_signal.py                                                                                                                                     
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
3 argument. Các arguments khác sẽ cần phải đẩy vào
% python test_signal.py                                                                                                                                     
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
^C('Signal Number:', 2, ' Frame: ', )
Press ctrl + c
4.