isolating thread-unsafe modules

Bob MacCallum uncoolbob at gmail.com
Mon Jul 29 12:29:05 BST 2013


Hadn't seen 'forks'...

I got forks to install on one machine (both perls 5.12.3. and 5.18.0) but
not another (also 5.12.3) which just hangs after this test error message:

t/forks01.t ............... perl: mg.c:94: S_save_magic: Assertion
`((sv)->sv_flags & (0x00200000|0x00400000|0x00800000))' failed.


But where it did install,  I trivially tried to see if Thread::Queue would
play nicely with forks.  I thought it didn't but after reading the docs I
got it to work!
(excuse the lack of "my" - this is cut and pasted from my perl shell)

use forks;
use forks::shared;
use Thread::Queue;

$queue = Thread::Queue->new()
share($queue);
$thr = threads->create( sub { while (my $job_spec = $queue->dequeue()) {
print "got $job_spec\n" } } );
$queue->enqueue("hello world")


nice!







On Mon, Jul 29, 2013 at 7:54 AM, Ashley Hindmarsh <
ash+perl at best-scarper.co.uk> wrote:

> > Message: 3
> > Date: Sun, 28 Jul 2013 20:07:59 +0100
> > From: Bob MacCallum <uncoolbob at gmail.com>
> > Subject: isolating thread-unsafe modules
> > To: "London.pm Perl M[ou]ngers" <london.pm at london.pm.org>
> > Message-ID:
> >         <
> > CAFLiiKdbkM62+Wr79hyRS8h-SeR0E+QGiVj1NaHX48sTkY0Qvg at mail.gmail.com>
> > Content-Type: text/plain; charset=ISO-8859-1
> >
> > Sorry for the Perl question, but I'm a bit out of my depth...
> >
> > I have a Dancer app providing a web service, and I have another script
> > using the same Dancer config file and db schema (e.g. "use Dancer
> > qw/:script/").  This script watches the database (for changes made by the
> > web app) and does the "heavy lifting".  Now I want to parallelise the
> heavy
> > lifting with threads and it seems from the core dump and googling that
> > DBD::Pg is not thread safe (see the few lines of debugger output below).
> >
> > That's fine - I am happy to run only non-database code in my "worker
> > threads".  The trouble is that even if I do this, I think the
> > threads->create is threading the Dancer globals which include postgres
> > schema connections.
> >
> > I tried a few "no My::Schema; no DBD::Pg; no Dancer ..." declarations in
> > the block with the threads->create - but that didn't seem to help.
> >
> > Does anyone know how I can use thread-safe and thread-unsafe code in the
> > same script?  I think I just need to make a block where none of the
> > Dancer/database stuff is in scope?
> >
> > I thought Thread::Isolate might help, but it hangs on the first test
> during
> > the install.  It's dated 2005.
> >
> > thanks!
> > Bob.
> >
> >
> Does anyone use 'forks'?
>
> CPAN: "drop-in replacement for Perl threads using fork()"
>
> Latest: http://search.cpan.org/~rybskej/forks-0.34/
>
> It looks like it might do the trick, but I've never had cause to use it.
>
>    Ash
>


More information about the london.pm mailing list