Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method within a `signal` block?

Tags:

signals

raku

Is it possible to call a user defined method within a signal block?

method my-method ( ... ) {
    signal(SIGTERM,SIGINT,SIGQUIT,SIGHUP).tap( -> $sig {
        say "Received signal: $sig";
        self!restore-term();
        exit;
    } );
    ...
    ...
}
like image 551
sid_com Avatar asked Apr 19 '20 02:04

sid_com


People also ask

How to block a number or group on signal?

On your phone, go to Signal Settings > Privacy > Blocked. Steps to block a number or group: Open Signal on your phone. Open a chat with this contact or phone number. Tap on the chat header with the group name or contact name/number. Choose Block, Block this user, Block this group. Confirm by choosing Block. Choose Ok.

What is the purpose of signal blocking?

Signals may be blocked to allow critical sections of code to run without interruption. The following functions control signal blocking: block either SAS/C managed signals or USS signals. sigprocmask and sigsuspend are portable to POSIX-conforming systems.

How do you call a method from a function block?

You call a method using a pointer to a function block (for example, pFB^.SampleMethod). In this situation, the pointer can point to instances of the type of the function block and to instances of all derived function blocks.

How do I trigger a function call from a signal?

Attach a function-call initiator to the function-call input port. If you attach an Inport block, open the block, select the Signal Attributes tab, then select the Output function call check box. Configure a Function-Call Subsystem block by setting the Sample time type of its Trigger Port block to triggered or periodic.


1 Answers

Yes, but there are caveats.

signal provides a Supply of events, which you can tap (if you like to) but probably should be using inside a react whenever structure.

In your example, the tap takes a Block (lambda). Every time you call the method, it will set up another Supply (which is probably not what you want).

This block gets called whenever an event arrives. This means it is completely asynchronous and has no context, other than the lexical context in which the block was created.

So whenever a signal arrives in the Supply, it will run the block with the self at the moment the method got called. Which may not be what you expect?

So it feels to me you're asking the wrong question. Apparently you have an object (hopefully a sentinel) that you wish the restore-term method to be called on whenever someone indicates they want to leave the program. So you're probably better of restructuring your program to something like:

my $sentinel = class.new(...);
react {
    whenever signal(SIGTERM,SIGINT,SIGQUIT,SIGHUP) -> $sig {
        say "Received signal: $sig";
        $sentinel.restore-term();
        exit;
    }
    # .... other stuff
}
like image 173
Elizabeth Mattijsen Avatar answered Oct 19 '22 07:10

Elizabeth Mattijsen