Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: FileHandles and threads

by zentara (Cardinal)
on Sep 22, 2005 at 18:21 UTC ( [id://494239]=note: print w/replies, xml ) Need Help??


in reply to Re: FileHandles and threads
in thread FileHandles and threads

Thanks for that code example! Passing a filehandle through a thread shared variable was something I've been searching for for awhile. Just for demonstration purposes, it can go the other way too.....create the filehandle in the thread, and read it in main.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; my %shash; #share(%shash); #will work only for first level keys my %hash; share ($shash{'go'}); share ($shash{'fileno'}); share ($shash{'pid'}); share ($shash{'die'}); $shash{'go'} = 0; $shash{'fileno'} = -1; $shash{'pid'} = -1; $shash{'die'} = 0; $hash{'thread'} = threads->new(\&work); $shash{'go'} = 1; sleep 1; # cheap hack to allow thread to setup my $fileno = $shash{'fileno'}; open (my $fh, "<&=$fileno") or warn "$!\n"; while ( <$fh> ){ print "In main-> $_"; } #wait for keypress to keep main thread alive <>; # control-c to exit ################################################################## sub work{ $|++; while(1){ if($shash{'die'} == 1){ goto END }; if ( $shash{'go'} == 1 ){ my $pid = open(FH, "top -b |" ) or warn "$!\n"; my $fileno = fileno(FH); print "fileno->$fileno\n"; $shash{'fileno'} = $fileno; $shash{'go'} = 0; #turn off self before returning }else { sleep 1 } } END: } #####################################################################

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

Replies are listed 'Best First'.
Re^3: FileHandles and threads
by BrowserUk (Patriarch) on Sep 22, 2005 at 20:17 UTC

    I first played with this a couple of years ago. I first mentioned it here about a year ago in Re^3: Passing globs between threads.

    By using a pool of threads and passing the filenos via a queue, it is possible to efficiently emulate all the usual forms of forking server architecture.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-10-13 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.