in reply to Tk, threads, and mjpeg stream

So, yes, I'm replying to my own post :) I forgot about good 'ol Parallel::ForkManager. When I dont use the resources for Tk to draw everything, I see the threads starting and pushing data. However, when I ask them to output their contense to the screen, I now only get just one image and the following errors.
./cams-split.pl Start Loop Start Loop Start Loop Start Loop Zone 1 Zone 2 X Error of failed request: BadIDChoice (invalid resource ID chosen fo +r this connection) Major opcode of failed request: 55 (X_CreateGC) Resource id in failed request: 0x1e00009 Serial number of failed request: 171 Current serial number in output stream: 44 X Error of failed request: BadIDChoice (invalid resource ID chosen fo +r this connection) Major opcode of failed request: 55 (X_CreateGC) Resource id in failed request: 0x1e00009 Serial number of failed request: 177 Current serial number in output stream: 44 Zone 1 X Error of failed request: BadIDChoice (invalid resource ID chosen fo +r this connection) Major opcode of failed request: 55 (X_CreateGC) Resource id in failed request: 0x1e00009 Serial number of failed request: 387 Current serial number in output stream: 44 Zone 1 Zone 1 Zone 1 Zone 1
I have the debug code in so everyone can tell what is happening better. Here is the current codebase:
#!/usr/bin/perl # Origional: # Test program to decode the multipart-replace stream that # ZoneMinder sends. It's a hack for this stream only though # and could be easily improved. For example we ignore the # Content-Length. # # Mark J Cox, mark@awe.com, February 2006 # # Added support for multiple "monitors"... still in progress use Tk; use Tk::X11Font; use Tk::JPEG; use LWP::UserAgent; use MIME::Base64; use IO::Socket; use Parallel::ForkManager; my $fm = new Parallel::ForkManager(4); my $host = "someipaddress"; @links=( ["/cgi-bin/nph-zms?mode=jpeg&monitor=1&scale=100&maxfps=5&user=web&p +ass=webuser","1"], ["/cgi-bin/nph-zms?mode=jpeg&monitor=2&scale=100&maxfps=5&user=web&p +ass=webuser","2"], ["/cgi-bin/nph-zms?mode=jpeg&monitor=3&scale=100&maxfps=5&user=web&p +ass=webuser","3"], ["/cgi-bin/nph-zms?mode=jpeg&monitor=4&scale=100&maxfps=5&user=web&p +ass=webuser","4"] ); my $stop = 0; my $mw = MainWindow->new(title=>"Cams"); $mw->minsize( qw(640 480)); my $top = $mw->Frame()->pack(-side=>'top'); my $bottom = $mw->Frame()->pack(-side=>'bottom'); my $photo1 = $top->Label()->pack(-side => 'left'); my $photo2 = $top->Label()->pack(-side => 'right'); my $photo3 = $bottom->Label()->pack(-side => 'left'); my $photo4 = $bottom->Label()->pack(-side => 'right'); $mw->Button(-text=>"Stop",-command => sub { $stop=1; })->pack(); foreach my $itemarray (@links) { $fm->start and next; print "Start Loop \n"; my ($url,$zone) = @$itemarray; getdata($url,$zone); $fm->finish; } $fm->wait_all_children; MainLoop; sub getdata { my $url = shift; my $id = shift; return unless ($stop == 0); my $sock = IO::Socket::INET->new(PeerAddr=>$host,Proto=>'tcp',Peer +Port=>80,); return unless defined $sock; $sock->autoflush(1); print $sock "GET $url HTTP/1.0\r\nHost: $host\r\n\r\n"; my $status = <$sock>; die unless ($status =~ m|HTTP/\S+\s+200|); my ($grab,$jpeg,$data,$image,$thisbuf,$lastimage); while (my $nread = sysread($sock, $thisbuf, 4096)) { $grab .= $thisbuf; if ( $grab =~ s/(.*?)\n--ZoneMinderFrame\r\n//s ) { $jpeg .= $1; $jpeg =~ s/--ZoneMinderFrame\r\n//; # Heh, what a $jpeg =~ s/Content-Length: \d+\r\n//; # Nasty little $jpeg =~ s/Content-Type: \S+\r\n\r\n//; # Hack $data = encode_base64($jpeg); undef $jpeg; eval { $image = $mw->Photo(-format=>"jpeg",-data=>$data); }; undef $data; eval { print "Zone $id\n"; if ($id == 1) { $photo1->configure(-image=>$image); } elsif ($id == 2 ) { $photo2->configure(-image=>$image); } elsif ($id == 3 ) { $photo3->configure(-image=>$image); } elsif ($id == 4 ) { $photo4->configure(-image=>$image); } }; $lastimage->delete if ($lastimage); #essential as Photo le +aks! $lastimage = $image; } $jpeg .= $1 if ($grab =~ s/(.*)(?=\n)//s); last if $stop; $mw->update; } $stop = 0; }