Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

problem using fork in perl-tk

by osamaamin (Initiate)
on Nov 02, 2011 at 07:59 UTC ( #935319=perlquestion: print w/replies, xml ) Need Help??
osamaamin has asked for the wisdom of the Perl Monks concerning the following question:


i m building a GUI based script in which the user is prompted for some input. when a button is pressed, a long time consuming command starts which freezes the GUI. in order to overcome this problem, i tried using fork but the script crashes with the following error msg:

perl: xcb_io.c:221: poll_for_event: Assertion `(((long) (event_sequence) - (long) (dpy->request)) <= 0)' failed.

Abort (core dumped)

this is my general program structure:


when button pressed, fork a new process to do background processing so that gui not freeze.

notify the user when background processing completes (child process ends)


do some time consuming work


Replies are listed 'Best First'.
Re: problem using fork in perl-tk
by RichardK (Parson) on Nov 02, 2011 at 08:39 UTC
      how can i implement it without using threads (using fork) ?
Re: problem using fork in perl-tk
by zentara (Archbishop) on Nov 02, 2011 at 12:32 UTC
    You mention using a fork, but a piped open or IPC module like IPC::Open3 or IPC-System-Simple will do alot of the work for you. You can also make a set of pipes manually, fork off your code with read and write pipe handles handed off to the fork, but it's alot of details.

    One detail you did not mention. Is your background process contained in another script which you can call, or is it in a block of code?

    Here is a simple fork example to get you started, otherwise show some code. Also the Tk::ActivityBar module needs to be installed manually, it is used only for a quick demonstration, but it is a problem-prone module.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::ActivityBar; my $mw = MainWindow->new; my $activity = $mw->ActivityBar(-anchor => 'w')->pack(-expand => 1); $mw->Button(-text => 'Start Activity', -command => sub{ $activity->startActivity(); #start the bar my $timer = $mw->after(6500, sub{ $activity->configure('-value' => 0); } ); &do_it; })->pack(); $mw->Button(-text => 'Exit', -command =>sub{ exit })->pack(); MainLoop(); sub do_it{ # you need to fork your command or it will block the gui # from functioning if(fork == 0){ system("date; sleep 2; date; sleep 2; date; sleep 2"); } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://935319]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (15)
As of 2018-06-22 15:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.