0

I am working on a PHP script that instantiates three objects of the same class with different parameters. These objects in turn run methods for infinite duration (like a Cron Job), and hence, the next line or the 2nd never begins.

So if I have

$a = new CronJob('Listing', '2010-04-01T00:00:01', '2010-04-01T00:30:01', 'A', 'BUSO');
$s = new CronJob('Listing', '2010-04-01T00:00:01', '2010-04-01T00:30:01', 'S', 'BUSO');
$p = new CronJob('Listing', '2010-04-01T00:00:01', '2010-04-01T00:30:01', 'P', 'BUSO');

$s is never executed since $a is running for infinite duration

How can I get around this? I thought of running them in different scripts but I don't wanna make 3 * (#object types) to run them simultaenously.

jmishra
  • 2,086
  • 2
  • 24
  • 38

2 Answers2

1

Honestly, your best bet will be to make separate cron jobs entries. PHP does not natively support multi-threading and many of the hacks out there to get it to work are costly.

Take a look at this stack overflow question: How can one use multi threading in PHP applications

Update:

Wanted to also throw the idea out there for Gearman. If you can add external extensions, download the PECL package and install it. You will need the gearman bin's on the local system.

Community
  • 1
  • 1
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
0

Have you considered PHP threading?

Here's another SO discussion on PHP threading issues.

Community
  • 1
  • 1
curtisdf
  • 4,130
  • 4
  • 33
  • 42
  • Care to comment on why my answer is bad? The OP wants to do multiple long-running things at once, so why not threading? – curtisdf Jun 20 '12 at 14:47
  • I am guessing because Pcntl_fork will create a new child pid, and only that. Any blocking which occurs in the parent thread is still present in the child thread. This defeats the purpose of attempting to multitask. You should really look into semiphors. – Mike Mackintosh Jun 21 '12 at 01:44