triggering callback from "outside"

H

Hannes

Hi,

does anybody know if it is possible to trigger a callback from the "outside"
(another process).
The only method I know of, are (unix) signals, which won't work for me
(since signal handling is delayed in Perl/Tk MainLoop, and won't work on
win32 platforms).

Hannes
 
B

Ben Morrow

Hannes said:
does anybody know if it is possible to trigger a callback from the "outside"
(another process).
The only method I know of, are (unix) signals, which won't work for me
(since signal handling is delayed in Perl/Tk MainLoop, and won't work on
win32 platforms).

It depends on what the receiving process is waiting for... you can use
sockets (probably best for portablility), fifos... read perldoc perlipc
for some idea of the possible mechanisms.

Ben
 
C

ctcgag

Hannes said:
Hi,

does anybody know if it is possible to trigger a callback from the
"outside" (another process).

Isn't the point of a callback that the outside process decides when *it*
wants to trigger it? If you want to tell the outside process what to do,
isn't that just an ordinary call?

Maybe I don't understand the question...
The only method I know of, are (unix) signals, which won't work for me
(since signal handling is delayed in Perl/Tk MainLoop, and won't work on
win32 platforms).

Xho
 
A

Ala Qumsieh

Hannes said:
The only method I know of, are (unix) signals, which won't work for me
(since signal handling is delayed in Perl/Tk MainLoop, and won't work on
win32 platforms).

As I suggested in comp.lang.perl.tk, why don't you use sockets? Did you
try that approach?

I would make the Tk process the server, and listen (in a non-blocking
manner) at a certain port and wait for data to be read. Then any other
app can connect to that port and trigger the callback by sending the
correct data.

--Ala
 
H

Hannes

I would make the Tk process the server, and listen (in a non-blocking
manner) at a certain port and wait for data to be read. Then any other
app can connect to that port and trigger the callback by sending the
correct data.

Is there a way in Perl to get a callback triggered by incoming data?
Keeping the process busy by polling the non-blocking socket in an
idle-callback wouldn't be very good...

Hannes
 
A

Ala Qumsieh

Hannes said:
Is there a way in Perl to get a callback triggered by incoming data?
Keeping the process busy by polling the non-blocking socket in an
idle-callback wouldn't be very good...

Why not? That's very similar to what Tk::MainLoop() is doing. You have
to poll to find something (unless you're using signals, but in your case
you can't).

For your purposes, I would replace the call to Tk::MainLoop with
something like the following [untested - adapted from example in perldoc
IO::Select]:

use Tk qw/:eventtypes/;
use IO::Select;
use IO::Socket;

my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 12345);
my $sel = new IO::Select($lsn);

while (1) { # this is your mainloop
# process any Tk events like mouse moves/button presses/etc
1 while $MW->DoOneEvent(ALL_EVENTS|DONT_WAIT);

# poll for any data in the sockets in a non-blocking way
if (my @ready = $sel->can_read(0.1)) {
for my $fh (@ready) {
if ($fh == $lsn) { # new connection
my $new = $lsn->accept;
$sel->add($new);
} else {
# somebody sent something
my $data = <$fh>;
if (defined $data) {
#
# trigger your callback based on what $data contains.
#
} else { # hung up
$sel->remove($fh);
$fh->close;
}
}
}
}
}

Now your clients can connect to port 12345 on the local machine and send
whatever data is needed to trigger the callbacks.

--Ala
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top