PhillyR has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; use Term::ReadKey; use threads; $|++; ReadMode('cbreak'); # works non-blocking if read stdin is in a thread my $count = 0; my $thr = threads->new(\&read_in)->detach; while(1){ print "test, count:$count\n"; sleep 1; if ($count == 10) { my $thr_proc = threads->new(\&proc_it)->detach; } $count++; } ReadMode('normal'); # restore normal tty settings sub proc_it{ while(1) { print "@"; sleep 2; } } sub read_in{ while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "\t\t$char->", ord($char),"\n"; if($char eq 'q'){exit} } } } __END__
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Non-blocking IO and threads
by runrig (Abbot) on Oct 06, 2011 at 19:00 UTC | |
by PhillyR (Acolyte) on Oct 06, 2011 at 19:38 UTC | |
|
Re: Non-blocking IO and threads
by BrowserUk (Patriarch) on Oct 06, 2011 at 17:11 UTC | |
by PhillyR (Acolyte) on Oct 06, 2011 at 18:03 UTC | |
by BrowserUk (Patriarch) on Oct 06, 2011 at 18:36 UTC | |
by clueless newbie (Curate) on Oct 06, 2011 at 18:32 UTC | |
|
Re: Non-blocking IO and threads
by Anonymous Monk on Oct 11, 2011 at 05:32 UTC |