use strict; use warnings; use threads (stack_size => 64*4096); use IO::Handle (); # extra stability my @car_links = (1..200); my ($username, $password, $fh, @known_cars); sub reserve_car { } sub write_url_to_known_cars { } my $count = 0; for my $c (@car_links) { CREATE: my $thr = threads->create(sub { # Reserve for 10 seconds total. for (1..1) { reserve_car($c, $username, $password); sleep 10; } threads->exit(0); }); if (!defined $thr) { while () { $count++; warn "cannot spawn thread, waiting -- $count\n"; my $thr_exited = 0; for my $thr (threads->list(threads::joinable)) { $thr->join; $thr_exited = 1; } $thr_exited ? last : sleep(1); } goto CREATE; } push @known_cars, $c; write_url_to_known_cars($c, $fh); } print "reaping threads\n"; $_->join for threads->list; #### use strict; use warnings; use POSIX ":sys_wait_h"; use IO::Handle (); # extra stability my @car_links = (1..100); my ($username, $password, $fh, @known_cars); sub reserve_car { } sub write_url_to_known_cars { } my $count = 0; my %pids; for my $c (@car_links) { FORK: my $pid = fork(); if (!defined $pid) { while () { $count++; warn "cannot spawn child, waiting -- $count\n"; my $child_exited = 0; for my $pid (keys %pids) { # support negative PID value on Windows my $ret = waitpid($pid, WNOHANG); if ($ret < -1 || $ret > 0) { delete $pids{$pid}; $child_exited = 1; } } $child_exited ? last : sleep(1); } goto FORK; } elsif ($pid == 0) { # Reserve for 10 seconds total. for (1..1) { reserve_car($c, $username, $password); sleep 10; } exit; } else { $pids{$pid} = undef; } push @known_cars, $c; write_url_to_known_cars($c, $fh); } print "reaping children\n"; waitpid($_, 0) for (keys %pids);