Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Need help in using threads in Tk

by mailmeakhila (Sexton)
on Apr 27, 2012 at 02:53 UTC ( [id://967501]=perlquestion: print w/replies, xml ) Need Help??

mailmeakhila has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am very new to threads. I have a well built script running properly.It takes too long to process. So i want to use threads.Below is my script on a small scale."Free to wrong pool a4b73c0 not b3bf050 at C:/strawberry/perl/site/lib/Parallel/ForkManager.pm line 498, <FH> line 6." error and perl.exe stops saying encountered a problem. Any help is really appreciated.
#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Pane; use WWW::Mechanize; use Web::Scraper; use Data::Dumper; use Parallel::ForkManager; my $mw = tkinit(); $mw->geometry('300x300+100+100'); my $sp = $mw->Scrolled('Pane',-scrollbars=>'osoe',sticky=>'nwse')->pac +k(-expand=>1,-fill=>'both'); $sp->Label(-text=>"Give Keyword file")->pack(); my $entry1 = $sp->Entry()->pack(); my $showbutton = $mw->Button(-text=>'Submit', -command =>\&button_callback)->pack(); my $bimage = scraper { process "img" , "url[]" => '@src'; }; sub button_callback { my $filename = $entry1->get(); open FH, $filename or die $!; my $mech = WWW::Mechanize->new; my $manager = new Parallel::ForkManager(10); while (my $line = <FH>) { $manager->start and next; chomp($line); my @temp = split(/ /,$line); my $query = join('+',@tem +p); my $ibing = "http://www.bing.com/images/search?count=10&q=".$query; $mech->get($ibing); #print Dumper $mech->content; my $res = $bimage->scrape($mech->content,$mech->uri); print Dumper $res; $manager->finish; $manager->wait_all_children; } close (FH); } MainLoop();

Replies are listed 'Best First'.
Re: Need help in using threads in Tk
by nikosv (Deacon) on Apr 27, 2012 at 05:47 UTC
    Tk is not thread safe,so typically you must start the threads before initializing Tk and run Tk in its own thread. Then you must poll from the main thread to get the reusults of the children.
Re: Need help in using threads in Tk
by zentara (Archbishop) on Apr 27, 2012 at 10:14 UTC
    I don't see threads being used anywhere in your script, it uses Parallel::ForkManager. The "free to wrong pool" error may be a Windows related bug in Parallel::ForkManager, since on Windows, forks are emulated with threads. But you are the first I've seen reporting that as a thread-related problem running it with Tk.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Hi I am new to threading. So i used Parallel::ForkManager as was suggested that i can use that module for forking of parallel downloads in torrents. My situation was similar to that so i tried it. Can you suggest any changes or modules i can have success. I have also tried the below script.
      #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Pane; use WWW::Mechanize; use Web::Scraper; use Data::Dumper; use threads; my $mw = tkinit(); $mw->geometry('300x300+100+100'); my $sp = $mw->Scrolled('Pane',-scrollbars=>'osoe',sticky=>'nwse')->pac +k(-expand=>1,-fill=>'both'); $sp->Label(-text=>"Give Keyword file")->pack(); my $entry1 = $sp->Entry()->pack(); my $showbutton = $mw->Button(-text=>'Submit', -command =>\&button_callback)->pack(); my $bimage = scraper { process "img" , "url[]" => '@src'; }; sub button_callback { my $filename = $entry1->get(); open FH, $filename or die $!; while (my $tmpline = <FH>) { my $thr1 = threads->new(\&threadsub($tmpline)); if(threads->list(threads::running)) { print "Waiting for all threads to complete running"; } $thr1->join(); } sub threadsub { my $line =$_; chomp($line); my @temp = split(/ /,$line); my $query = join('+',@tem +p); our $ibing = "http://www.bing.com/images/search?count=10&q=".$query; my $mech = WWW::Mechanize->new; $mech->get($ibing); #print Dumper $mech->content; my $res = $bimage->scrape($mech->content,$mech->uri); print Dumper $res; } close (FH); } MainLoop();
      and here is my error: Use of uninitialized value $line in scalar chomp at btest.pl line 34, <FH> line 1. Use of uninitialized value $line in split at btest.pl line 34, <FH> line 1. $VAR1 = {}; Waiting for all threads to complete runningThread 1 terminated abnormally: Not a CODE reference at btest.pl line 26. Free to wrong pool 333f7f0 not 388020 at C:/strawberry/perl/site/lib/Tk/Widget.p m line 98 during global destruction.
        Your big problem as far as threads and Tk goes, is that you are trying to start threads after Tk code has been written. Tk is not threadsafe. It looks like you should be able to reorganize your code fairly easily. See PerlTk on a thread... for a simple Th with threads example, and some rules to follow. Also, if you are on a single cpu machine, using threads probably will not increase your performance.

        So, create your thread first, before tkinit, then confine all your network code to the thread.

        By the way, not to confuse you further, but Perl/Gtk2 will allow you to create threads after the GUI code is init'ed, but it still is a good rule to keep thread code and GUI code separate.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh
Re: Need help in using threads in Tk
by Anonymous Monk on Apr 27, 2012 at 05:29 UTC

    Hi,

    Use Super Search ( see top of this page ). Put in 'threads Tk' and you will get lots of help.

    J.C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://967501]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found