Simbi
Simbi

Reputation: 1042

Can I raise a signal from python?

The topic basically tells what I want to to.

I read the documentation, which tells me how to handle signals but not how I can do signalling by myself.

Thanks!

Upvotes: 18

Views: 20296

Answers (4)

Ronen Ness
Ronen Ness

Reputation: 10760

Just proposing a different method, if its on windows:

import ctypes
ucrtbase = ctypes.CDLL('ucrtbase')
c_raise = ucrtbase['raise']
c_raise(some_signal)

some_signal can be any signal num, eg signal.SIGINT.

Upvotes: 3

Campa
Campa

Reputation: 4505

Directly with the signal package (new in version 3.8). For instance:

import signal
signal.raise_signal( signal.SIGINT )

Reference: https://docs.python.org/3/library/signal.html#signal.raise_signal

Upvotes: 14

helmbert
helmbert

Reputation: 38044

You can use the os.kill method. Since Python 2.7 it should work (did not test it myself) on both Unix and Windows, although it needs to be called with different parameters:

import os, signal

os.kill(pid, signal.SIGHUP) # Unix version only...

Upvotes: 13

phihag
phihag

Reputation: 288270

Use os.kill. For example, to send SIGUSR1 to your own process, use

import os,signal
os.kill(os.getpid(), signal.SIGUSR1)

Upvotes: 30

Related Questions