Python and signal handlers












2















I am in need for some clarification regarding signal handlers in python, as I do not precisely understand how they work, how I can use them and what the limitations are.



I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.



I found that, as intended, the signals I send seem to be processed immediately in an asynchronous way.



Therefore, I used to think that the registered signal handlers run in their own threads which, I thought, would explain why the following code will print multiple lines of Signal handler called with signal 10 at once when sending signals in a loop



#!/usr/bin/python3.5
# This is the file signal_example.py

import signal, time, os

def handler(signum, frame):
print('Signal handler called with signal', signum)
time.sleep(20)

signal.signal(signal.SIGUSR1, handler)

time.sleep(100)


#!/usr/bin/bash
for ((i=0;i<100;i=i+1)); do
killall -s SIGUSR1 signal_example.py;
done


However, documentation states (https://docs.python.org/3.4/library/signal.html) "Python signal handlers are always executed in the main Python thread, even if the signal was received in another thread.". Also, I do not see separate threads running on my system for the above example.



I am now wondering, what the actual implications and limitations of my approach to using signals are and how I can properly do relevant things such as communication between instances of the invoked signal handlers.



For example, in certain cases, I would like to be able to delay the execution of some code inside a signal handler until previous signals of the same kind have been processed.
Also I do not understand how many signals will be processed "in parallel" and what happens when the "queue" is full ...



I have been looking a bit into Pyhton's asyncio, which seems to offer some control of asynchronous code, and also brings its own way to register signal handlers. It seems to offer some aids, however, it does not seem to help me a lot since the behavior that I see (signals beeing handled pretty much when they are received) is actually what I want. The use of asyncio signal handlers that I have seen appears to execute the signals in the event loop. In the partially blocking code that I am starting with, that could be too late. I could run some of it in separate threads (using the corresponding asyncio functions), but not make the entire code non-blocking.










share|improve this question



























    2















    I am in need for some clarification regarding signal handlers in python, as I do not precisely understand how they work, how I can use them and what the limitations are.



    I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.



    I found that, as intended, the signals I send seem to be processed immediately in an asynchronous way.



    Therefore, I used to think that the registered signal handlers run in their own threads which, I thought, would explain why the following code will print multiple lines of Signal handler called with signal 10 at once when sending signals in a loop



    #!/usr/bin/python3.5
    # This is the file signal_example.py

    import signal, time, os

    def handler(signum, frame):
    print('Signal handler called with signal', signum)
    time.sleep(20)

    signal.signal(signal.SIGUSR1, handler)

    time.sleep(100)


    #!/usr/bin/bash
    for ((i=0;i<100;i=i+1)); do
    killall -s SIGUSR1 signal_example.py;
    done


    However, documentation states (https://docs.python.org/3.4/library/signal.html) "Python signal handlers are always executed in the main Python thread, even if the signal was received in another thread.". Also, I do not see separate threads running on my system for the above example.



    I am now wondering, what the actual implications and limitations of my approach to using signals are and how I can properly do relevant things such as communication between instances of the invoked signal handlers.



    For example, in certain cases, I would like to be able to delay the execution of some code inside a signal handler until previous signals of the same kind have been processed.
    Also I do not understand how many signals will be processed "in parallel" and what happens when the "queue" is full ...



    I have been looking a bit into Pyhton's asyncio, which seems to offer some control of asynchronous code, and also brings its own way to register signal handlers. It seems to offer some aids, however, it does not seem to help me a lot since the behavior that I see (signals beeing handled pretty much when they are received) is actually what I want. The use of asyncio signal handlers that I have seen appears to execute the signals in the event loop. In the partially blocking code that I am starting with, that could be too late. I could run some of it in separate threads (using the corresponding asyncio functions), but not make the entire code non-blocking.










    share|improve this question

























      2












      2








      2


      2






      I am in need for some clarification regarding signal handlers in python, as I do not precisely understand how they work, how I can use them and what the limitations are.



      I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.



      I found that, as intended, the signals I send seem to be processed immediately in an asynchronous way.



      Therefore, I used to think that the registered signal handlers run in their own threads which, I thought, would explain why the following code will print multiple lines of Signal handler called with signal 10 at once when sending signals in a loop



      #!/usr/bin/python3.5
      # This is the file signal_example.py

      import signal, time, os

      def handler(signum, frame):
      print('Signal handler called with signal', signum)
      time.sleep(20)

      signal.signal(signal.SIGUSR1, handler)

      time.sleep(100)


      #!/usr/bin/bash
      for ((i=0;i<100;i=i+1)); do
      killall -s SIGUSR1 signal_example.py;
      done


      However, documentation states (https://docs.python.org/3.4/library/signal.html) "Python signal handlers are always executed in the main Python thread, even if the signal was received in another thread.". Also, I do not see separate threads running on my system for the above example.



      I am now wondering, what the actual implications and limitations of my approach to using signals are and how I can properly do relevant things such as communication between instances of the invoked signal handlers.



      For example, in certain cases, I would like to be able to delay the execution of some code inside a signal handler until previous signals of the same kind have been processed.
      Also I do not understand how many signals will be processed "in parallel" and what happens when the "queue" is full ...



      I have been looking a bit into Pyhton's asyncio, which seems to offer some control of asynchronous code, and also brings its own way to register signal handlers. It seems to offer some aids, however, it does not seem to help me a lot since the behavior that I see (signals beeing handled pretty much when they are received) is actually what I want. The use of asyncio signal handlers that I have seen appears to execute the signals in the event loop. In the partially blocking code that I am starting with, that could be too late. I could run some of it in separate threads (using the corresponding asyncio functions), but not make the entire code non-blocking.










      share|improve this question














      I am in need for some clarification regarding signal handlers in python, as I do not precisely understand how they work, how I can use them and what the limitations are.



      I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.



      I found that, as intended, the signals I send seem to be processed immediately in an asynchronous way.



      Therefore, I used to think that the registered signal handlers run in their own threads which, I thought, would explain why the following code will print multiple lines of Signal handler called with signal 10 at once when sending signals in a loop



      #!/usr/bin/python3.5
      # This is the file signal_example.py

      import signal, time, os

      def handler(signum, frame):
      print('Signal handler called with signal', signum)
      time.sleep(20)

      signal.signal(signal.SIGUSR1, handler)

      time.sleep(100)


      #!/usr/bin/bash
      for ((i=0;i<100;i=i+1)); do
      killall -s SIGUSR1 signal_example.py;
      done


      However, documentation states (https://docs.python.org/3.4/library/signal.html) "Python signal handlers are always executed in the main Python thread, even if the signal was received in another thread.". Also, I do not see separate threads running on my system for the above example.



      I am now wondering, what the actual implications and limitations of my approach to using signals are and how I can properly do relevant things such as communication between instances of the invoked signal handlers.



      For example, in certain cases, I would like to be able to delay the execution of some code inside a signal handler until previous signals of the same kind have been processed.
      Also I do not understand how many signals will be processed "in parallel" and what happens when the "queue" is full ...



      I have been looking a bit into Pyhton's asyncio, which seems to offer some control of asynchronous code, and also brings its own way to register signal handlers. It seems to offer some aids, however, it does not seem to help me a lot since the behavior that I see (signals beeing handled pretty much when they are received) is actually what I want. The use of asyncio signal handlers that I have seen appears to execute the signals in the event loop. In the partially blocking code that I am starting with, that could be too late. I could run some of it in separate threads (using the corresponding asyncio functions), but not make the entire code non-blocking.







      python signals python-multithreading python-asyncio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 12 '18 at 9:23









      highsciguyhighsciguy

      1,14121948




      1,14121948
























          1 Answer
          1






          active

          oldest

          votes


















          2















          I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.




          That sounds like a really bad idea. Here are several reasons why:




          • Unix signals are delivered asynchronously, which means you can get a signal while any library code is running, such as in the middle of a malloc call. For this reason, only a small number of functions are async-signal-safe, i.e. safe to invoke from a signal handler. Python code cannot execute inside a signal handler at all, so the handler set by signal.signal doesn't execute the Python function, but simply sets a global flag. This flag is checked by the main thread from time to time, which executes the Python handler function. This means that there is no guarantee that the signal will be delivered immediately, i.e. you cannot rely on the signal delivery guarantees provided by the operating system.


          • A signal may arrive during the execution of a handler of a previous signal. This is why you are seeing multiple lines being printed, it is the artifact of the signal handler itself being interrupted by a signal and reentering.


          • Unless you set up signal handlers using specialized POSIX calls (and handlers set up using those calls can't directly execute Python code), signals will not be queued. Even when they are queued, except for non-real-time signals, the order of signals between generation and delivery is not preserved.


          • Signals interact really badly with multi-threaded code, even in pure C.



          If you have a service that you need to communicate with, you could have a thread that reads from a named pipe or listens on a socket. Writing to the pipe, or opening the socket, would serve as a signal to the process. This also allows passing additional information to the service.






          share|improve this answer


























          • Thanks for the answer. The picture that I get from your description is that the binary code generated from my Python script contains explicit checks for that signal flag. I am assuming that the signal handler would not interrupt the malloc (you seem to refer to UNIX there), but potentially between atomic Python instructions. The reception of a signal does not stop the running signal handlers, but it can interfer with them and the main program flow. An obvious question to ask is if it makes sense to consider starting a new thread manually in each of my signal handlers.

            – highsciguy
            Sep 13 '18 at 7:29











          • If you have, could you point me to a reference where pipes or sockets are used for inter-process communication with a python script?

            – highsciguy
            Sep 13 '18 at 7:30











          • @highsciguy A signal handler would definitely interrupt malloc - the alternative would be for each malloc to block signals and restore them, which would require two roundtrips to the kernel and impact performance. But you don't care about that in Python because (as discussed in the answer) Python doesn't run your code directly off the handler.

            – user4815162342
            Sep 13 '18 at 8:37













          • Perhaps I still don't understand. I have been thinking that you mean to say that the UNIX-signal handler (low-level) would interrupt malloc, but the invoked Python handler function would run only when malloc is done (as soon as it sees that the signal handler flag is set). I would assume that with the first effect of a signal malloc would need to cope since it is a standard UNIX-think that many processes implement.

            – highsciguy
            Sep 13 '18 at 10:58











          • @highsciguy That is what I mean to say, yes. The point is that the signal handlers registered in Python will run later than those registered in C. In other words, if raw speed of delivery is the motivation for using signals, you won't see it in Python.

            – user4815162342
            Sep 13 '18 at 11:13













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52291768%2fpython-and-signal-handlers%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2















          I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.




          That sounds like a really bad idea. Here are several reasons why:




          • Unix signals are delivered asynchronously, which means you can get a signal while any library code is running, such as in the middle of a malloc call. For this reason, only a small number of functions are async-signal-safe, i.e. safe to invoke from a signal handler. Python code cannot execute inside a signal handler at all, so the handler set by signal.signal doesn't execute the Python function, but simply sets a global flag. This flag is checked by the main thread from time to time, which executes the Python handler function. This means that there is no guarantee that the signal will be delivered immediately, i.e. you cannot rely on the signal delivery guarantees provided by the operating system.


          • A signal may arrive during the execution of a handler of a previous signal. This is why you are seeing multiple lines being printed, it is the artifact of the signal handler itself being interrupted by a signal and reentering.


          • Unless you set up signal handlers using specialized POSIX calls (and handlers set up using those calls can't directly execute Python code), signals will not be queued. Even when they are queued, except for non-real-time signals, the order of signals between generation and delivery is not preserved.


          • Signals interact really badly with multi-threaded code, even in pure C.



          If you have a service that you need to communicate with, you could have a thread that reads from a named pipe or listens on a socket. Writing to the pipe, or opening the socket, would serve as a signal to the process. This also allows passing additional information to the service.






          share|improve this answer


























          • Thanks for the answer. The picture that I get from your description is that the binary code generated from my Python script contains explicit checks for that signal flag. I am assuming that the signal handler would not interrupt the malloc (you seem to refer to UNIX there), but potentially between atomic Python instructions. The reception of a signal does not stop the running signal handlers, but it can interfer with them and the main program flow. An obvious question to ask is if it makes sense to consider starting a new thread manually in each of my signal handlers.

            – highsciguy
            Sep 13 '18 at 7:29











          • If you have, could you point me to a reference where pipes or sockets are used for inter-process communication with a python script?

            – highsciguy
            Sep 13 '18 at 7:30











          • @highsciguy A signal handler would definitely interrupt malloc - the alternative would be for each malloc to block signals and restore them, which would require two roundtrips to the kernel and impact performance. But you don't care about that in Python because (as discussed in the answer) Python doesn't run your code directly off the handler.

            – user4815162342
            Sep 13 '18 at 8:37













          • Perhaps I still don't understand. I have been thinking that you mean to say that the UNIX-signal handler (low-level) would interrupt malloc, but the invoked Python handler function would run only when malloc is done (as soon as it sees that the signal handler flag is set). I would assume that with the first effect of a signal malloc would need to cope since it is a standard UNIX-think that many processes implement.

            – highsciguy
            Sep 13 '18 at 10:58











          • @highsciguy That is what I mean to say, yes. The point is that the signal handlers registered in Python will run later than those registered in C. In other words, if raw speed of delivery is the motivation for using signals, you won't see it in Python.

            – user4815162342
            Sep 13 '18 at 11:13


















          2















          I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.




          That sounds like a really bad idea. Here are several reasons why:




          • Unix signals are delivered asynchronously, which means you can get a signal while any library code is running, such as in the middle of a malloc call. For this reason, only a small number of functions are async-signal-safe, i.e. safe to invoke from a signal handler. Python code cannot execute inside a signal handler at all, so the handler set by signal.signal doesn't execute the Python function, but simply sets a global flag. This flag is checked by the main thread from time to time, which executes the Python handler function. This means that there is no guarantee that the signal will be delivered immediately, i.e. you cannot rely on the signal delivery guarantees provided by the operating system.


          • A signal may arrive during the execution of a handler of a previous signal. This is why you are seeing multiple lines being printed, it is the artifact of the signal handler itself being interrupted by a signal and reentering.


          • Unless you set up signal handlers using specialized POSIX calls (and handlers set up using those calls can't directly execute Python code), signals will not be queued. Even when they are queued, except for non-real-time signals, the order of signals between generation and delivery is not preserved.


          • Signals interact really badly with multi-threaded code, even in pure C.



          If you have a service that you need to communicate with, you could have a thread that reads from a named pipe or listens on a socket. Writing to the pipe, or opening the socket, would serve as a signal to the process. This also allows passing additional information to the service.






          share|improve this answer


























          • Thanks for the answer. The picture that I get from your description is that the binary code generated from my Python script contains explicit checks for that signal flag. I am assuming that the signal handler would not interrupt the malloc (you seem to refer to UNIX there), but potentially between atomic Python instructions. The reception of a signal does not stop the running signal handlers, but it can interfer with them and the main program flow. An obvious question to ask is if it makes sense to consider starting a new thread manually in each of my signal handlers.

            – highsciguy
            Sep 13 '18 at 7:29











          • If you have, could you point me to a reference where pipes or sockets are used for inter-process communication with a python script?

            – highsciguy
            Sep 13 '18 at 7:30











          • @highsciguy A signal handler would definitely interrupt malloc - the alternative would be for each malloc to block signals and restore them, which would require two roundtrips to the kernel and impact performance. But you don't care about that in Python because (as discussed in the answer) Python doesn't run your code directly off the handler.

            – user4815162342
            Sep 13 '18 at 8:37













          • Perhaps I still don't understand. I have been thinking that you mean to say that the UNIX-signal handler (low-level) would interrupt malloc, but the invoked Python handler function would run only when malloc is done (as soon as it sees that the signal handler flag is set). I would assume that with the first effect of a signal malloc would need to cope since it is a standard UNIX-think that many processes implement.

            – highsciguy
            Sep 13 '18 at 10:58











          • @highsciguy That is what I mean to say, yes. The point is that the signal handlers registered in Python will run later than those registered in C. In other words, if raw speed of delivery is the motivation for using signals, you won't see it in Python.

            – user4815162342
            Sep 13 '18 at 11:13
















          2












          2








          2








          I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.




          That sounds like a really bad idea. Here are several reasons why:




          • Unix signals are delivered asynchronously, which means you can get a signal while any library code is running, such as in the middle of a malloc call. For this reason, only a small number of functions are async-signal-safe, i.e. safe to invoke from a signal handler. Python code cannot execute inside a signal handler at all, so the handler set by signal.signal doesn't execute the Python function, but simply sets a global flag. This flag is checked by the main thread from time to time, which executes the Python handler function. This means that there is no guarantee that the signal will be delivered immediately, i.e. you cannot rely on the signal delivery guarantees provided by the operating system.


          • A signal may arrive during the execution of a handler of a previous signal. This is why you are seeing multiple lines being printed, it is the artifact of the signal handler itself being interrupted by a signal and reentering.


          • Unless you set up signal handlers using specialized POSIX calls (and handlers set up using those calls can't directly execute Python code), signals will not be queued. Even when they are queued, except for non-real-time signals, the order of signals between generation and delivery is not preserved.


          • Signals interact really badly with multi-threaded code, even in pure C.



          If you have a service that you need to communicate with, you could have a thread that reads from a named pipe or listens on a socket. Writing to the pipe, or opening the socket, would serve as a signal to the process. This also allows passing additional information to the service.






          share|improve this answer
















          I intend to use USR signals on linux in order to communicate with a python program running in the background as a service.




          That sounds like a really bad idea. Here are several reasons why:




          • Unix signals are delivered asynchronously, which means you can get a signal while any library code is running, such as in the middle of a malloc call. For this reason, only a small number of functions are async-signal-safe, i.e. safe to invoke from a signal handler. Python code cannot execute inside a signal handler at all, so the handler set by signal.signal doesn't execute the Python function, but simply sets a global flag. This flag is checked by the main thread from time to time, which executes the Python handler function. This means that there is no guarantee that the signal will be delivered immediately, i.e. you cannot rely on the signal delivery guarantees provided by the operating system.


          • A signal may arrive during the execution of a handler of a previous signal. This is why you are seeing multiple lines being printed, it is the artifact of the signal handler itself being interrupted by a signal and reentering.


          • Unless you set up signal handlers using specialized POSIX calls (and handlers set up using those calls can't directly execute Python code), signals will not be queued. Even when they are queued, except for non-real-time signals, the order of signals between generation and delivery is not preserved.


          • Signals interact really badly with multi-threaded code, even in pure C.



          If you have a service that you need to communicate with, you could have a thread that reads from a named pipe or listens on a socket. Writing to the pipe, or opening the socket, would serve as a signal to the process. This also allows passing additional information to the service.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 20:46

























          answered Sep 12 '18 at 20:58









          user4815162342user4815162342

          62.2k593145




          62.2k593145













          • Thanks for the answer. The picture that I get from your description is that the binary code generated from my Python script contains explicit checks for that signal flag. I am assuming that the signal handler would not interrupt the malloc (you seem to refer to UNIX there), but potentially between atomic Python instructions. The reception of a signal does not stop the running signal handlers, but it can interfer with them and the main program flow. An obvious question to ask is if it makes sense to consider starting a new thread manually in each of my signal handlers.

            – highsciguy
            Sep 13 '18 at 7:29











          • If you have, could you point me to a reference where pipes or sockets are used for inter-process communication with a python script?

            – highsciguy
            Sep 13 '18 at 7:30











          • @highsciguy A signal handler would definitely interrupt malloc - the alternative would be for each malloc to block signals and restore them, which would require two roundtrips to the kernel and impact performance. But you don't care about that in Python because (as discussed in the answer) Python doesn't run your code directly off the handler.

            – user4815162342
            Sep 13 '18 at 8:37













          • Perhaps I still don't understand. I have been thinking that you mean to say that the UNIX-signal handler (low-level) would interrupt malloc, but the invoked Python handler function would run only when malloc is done (as soon as it sees that the signal handler flag is set). I would assume that with the first effect of a signal malloc would need to cope since it is a standard UNIX-think that many processes implement.

            – highsciguy
            Sep 13 '18 at 10:58











          • @highsciguy That is what I mean to say, yes. The point is that the signal handlers registered in Python will run later than those registered in C. In other words, if raw speed of delivery is the motivation for using signals, you won't see it in Python.

            – user4815162342
            Sep 13 '18 at 11:13





















          • Thanks for the answer. The picture that I get from your description is that the binary code generated from my Python script contains explicit checks for that signal flag. I am assuming that the signal handler would not interrupt the malloc (you seem to refer to UNIX there), but potentially between atomic Python instructions. The reception of a signal does not stop the running signal handlers, but it can interfer with them and the main program flow. An obvious question to ask is if it makes sense to consider starting a new thread manually in each of my signal handlers.

            – highsciguy
            Sep 13 '18 at 7:29











          • If you have, could you point me to a reference where pipes or sockets are used for inter-process communication with a python script?

            – highsciguy
            Sep 13 '18 at 7:30











          • @highsciguy A signal handler would definitely interrupt malloc - the alternative would be for each malloc to block signals and restore them, which would require two roundtrips to the kernel and impact performance. But you don't care about that in Python because (as discussed in the answer) Python doesn't run your code directly off the handler.

            – user4815162342
            Sep 13 '18 at 8:37













          • Perhaps I still don't understand. I have been thinking that you mean to say that the UNIX-signal handler (low-level) would interrupt malloc, but the invoked Python handler function would run only when malloc is done (as soon as it sees that the signal handler flag is set). I would assume that with the first effect of a signal malloc would need to cope since it is a standard UNIX-think that many processes implement.

            – highsciguy
            Sep 13 '18 at 10:58











          • @highsciguy That is what I mean to say, yes. The point is that the signal handlers registered in Python will run later than those registered in C. In other words, if raw speed of delivery is the motivation for using signals, you won't see it in Python.

            – user4815162342
            Sep 13 '18 at 11:13



















          Thanks for the answer. The picture that I get from your description is that the binary code generated from my Python script contains explicit checks for that signal flag. I am assuming that the signal handler would not interrupt the malloc (you seem to refer to UNIX there), but potentially between atomic Python instructions. The reception of a signal does not stop the running signal handlers, but it can interfer with them and the main program flow. An obvious question to ask is if it makes sense to consider starting a new thread manually in each of my signal handlers.

          – highsciguy
          Sep 13 '18 at 7:29





          Thanks for the answer. The picture that I get from your description is that the binary code generated from my Python script contains explicit checks for that signal flag. I am assuming that the signal handler would not interrupt the malloc (you seem to refer to UNIX there), but potentially between atomic Python instructions. The reception of a signal does not stop the running signal handlers, but it can interfer with them and the main program flow. An obvious question to ask is if it makes sense to consider starting a new thread manually in each of my signal handlers.

          – highsciguy
          Sep 13 '18 at 7:29













          If you have, could you point me to a reference where pipes or sockets are used for inter-process communication with a python script?

          – highsciguy
          Sep 13 '18 at 7:30





          If you have, could you point me to a reference where pipes or sockets are used for inter-process communication with a python script?

          – highsciguy
          Sep 13 '18 at 7:30













          @highsciguy A signal handler would definitely interrupt malloc - the alternative would be for each malloc to block signals and restore them, which would require two roundtrips to the kernel and impact performance. But you don't care about that in Python because (as discussed in the answer) Python doesn't run your code directly off the handler.

          – user4815162342
          Sep 13 '18 at 8:37







          @highsciguy A signal handler would definitely interrupt malloc - the alternative would be for each malloc to block signals and restore them, which would require two roundtrips to the kernel and impact performance. But you don't care about that in Python because (as discussed in the answer) Python doesn't run your code directly off the handler.

          – user4815162342
          Sep 13 '18 at 8:37















          Perhaps I still don't understand. I have been thinking that you mean to say that the UNIX-signal handler (low-level) would interrupt malloc, but the invoked Python handler function would run only when malloc is done (as soon as it sees that the signal handler flag is set). I would assume that with the first effect of a signal malloc would need to cope since it is a standard UNIX-think that many processes implement.

          – highsciguy
          Sep 13 '18 at 10:58





          Perhaps I still don't understand. I have been thinking that you mean to say that the UNIX-signal handler (low-level) would interrupt malloc, but the invoked Python handler function would run only when malloc is done (as soon as it sees that the signal handler flag is set). I would assume that with the first effect of a signal malloc would need to cope since it is a standard UNIX-think that many processes implement.

          – highsciguy
          Sep 13 '18 at 10:58













          @highsciguy That is what I mean to say, yes. The point is that the signal handlers registered in Python will run later than those registered in C. In other words, if raw speed of delivery is the motivation for using signals, you won't see it in Python.

          – user4815162342
          Sep 13 '18 at 11:13







          @highsciguy That is what I mean to say, yes. The point is that the signal handlers registered in Python will run later than those registered in C. In other words, if raw speed of delivery is the motivation for using signals, you won't see it in Python.

          – user4815162342
          Sep 13 '18 at 11:13




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52291768%2fpython-and-signal-handlers%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre