Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Perl Tk nonblocking (threads queue)

by Anonymous Monk
on Nov 30, 2016 at 23:28 UTC ( [id://1176998]=note: print w/replies, xml ) Need Help??


in reply to Perl Tk nonblocking

Try threaded version, should work, works on windows where fileevent wont work
#!/usr/bin/perl -- use threads stack_size => 4096; use threads::shared; use Thread::Queue; use strict; use warnings; { my $qin = Thread::Queue->new(); my $qout = Thread::Queue->new(); my $guithread = threads->create( \&tkguithread, $qin, $qout ); my $nmapthread = threads->create( \&nmapthread, $qin, $qout ); $guithread->join; } exit; sub tkguithread { my( $qin, $qout ) = @_; require Tk; my $mw = Tk::tkinit(); my $b1 = $mw->Button(-text => 'Go', -command => sub { $qin->enqueue( 'localhost' ); })->pack(); my $b2 = $mw->Button(-text => 'Exit', -command => [ $mw, 'destroy' + ] )->pack(); my $text = $mw->Scrolled('Text',-scrollbars=>'e')->pack; $mw->repeat( 500, #ms [ \&appendFromQToText, $qout, $text ], ); $mw->MainLoop; return; } sub appendFromQToText { my( $qout, $text ) = @_; for(1..5){ if( defined( my $line = $qout->dequeue_nb )){ $text->insert('end', $line ); } } return; } sub Sleeps(){ Time::HiRes::usleep( 33 * 1000 ); ## microseconds } sub nmapthread { my( $qin, $qout ) = @_; threads->detach(); ## can't join me :) while( 1 ) { #~ if( defined( my $url = $qin->dequeue_nb ) ) { if( defined( my $url = $qin->dequeue ) ) { nmap( $url, $qout ); } Sleeps(); } return; } sub nmap { my( $host , $qout ) = @_; $qout->enqueue("got nmap($host)\n"); $host =~ s{[^a-zA-Z0-9.]}{}g; $host = 'localhost' if ! length $host; #~ my $pid = open my($H), qq{nmap -A "$host" |}; my $pid = open my($H), qq{echo faker "$host" |}; $qout->enqueue("pid($pid) \$! @{[int$!]} $!\n"); $qout->enqueue("eof @{[eof($H)]}\n"); while(<$H>) { $qout->enqueue( "nmap($host) $_" ); } close $H; return; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found