Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get cross-thread communication in a Perl GTK program?
    text
    copied!<p>I have a Perl program that has a GTK2 GUI (via the <code>Gtk2</code> package). This program also opens a network socket (actually via <code>LWP</code>) in another thread, and continuously makes a request for a certain URL, waiting for an event to happen.</p> <p>If an event occurs, then its data must be processed and interpreted, and an appropriate callback function used to update the GUI. This is where my program falls down.</p> <p>Main program:</p> <pre><code># Attach to the "message received" event Foo::hook('msgRx', \&amp;updateMsg); # ... Gtk2-&gt;main(); sub updateMsg { my ($msg) = @_; print "New message: $msg\n"; # append to a GTK TextView -- code is also used elsewhere and works fine appendMsg($msg); } </code></pre> <p>And in the module:</p> <pre><code># ... my %hooks = (); my $ev_pid = undef; sub hook($&amp;) { my ($name, $sub) = @_; $hooks{$name} = $sub; } sub call_hook { my ($name, @args) = @_; print "&gt;&gt;&gt; CALLING HOOK $name\n"; return $hooks{$name}-&gt;(@args) if (defined($hooks{$name})); } sub eventThread { while (1) { my $res = $browser-&gt;post("$baseurl/events", ['id' =&gt; $convid]); my $content = $res-&gt;content; last if ($content eq 'null'); my $events = from_json($content); foreach (@$events) { my $ev_type = shift @$_; my @ev_args = @$_; print "Event type: $ev_type\n"; print Data::Dumper-&gt;Dump([@ev_args]); handleEvent($ev_type, @ev_args); } } } sub doConnect() { # ... $ev_pid = fork; if (!defined $ev_pid) { print "ERROR forking\n"; disconnect(); return; } if (!$ev_pid) { eventThread; exit; } } </code></pre> <p>Now the console output from these is what I expect:</p> <pre>>> Starting... [["connected"]] Event type: connected >>> CALLING HOOK start [["waiting"]] Event type: waiting >>> CALLING HOOK waiting [["gotMessage", "77564"]] Event type: gotMessage $VAR1 = '77564'; >>> CALLING HOOK msgRx New message: 77564 [["idle"]] Event type: idle >>> CALLING HOOK typing [["gotMessage", "816523"]] Event type: gotMessage $VAR1 = '816523'; >>> CALLING HOOK msgRx New message: 816523 >> User ending connection null >>> CALLING HOOK end</pre> <p>However, the GUI TextView does not update. I can only presume that this is because the callback is actually happening in another thread, which has duplicates of the objects.</p> <p>Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload