4

I have a cron file cron/cron1.php. i have set up this for cron running 1 minute.

so for next process it will take 1 minute to execute.

now i want to run this file parallel three times in minute. this file takes execution time more than 2 min.

can i run this file parallel in a single file like this

file1.php

<?php
      include("cron/cron1.php"); // run seperately
      sleep(5);
      include("cron/cron1.php"); // run seperately
       sleep(5);
      include("cron/cron1.php"); // run seperately 

?>

in above file cron1.php will execute 5 seconds difference but when above one is completed its process. as i told you each cron1.php will takes more than 2 minutes to complete. so i couldn't achieve it.

is there any process or multithreading or approch so that i can run each cron1.php every 5 seconds delay. then i will set the file1.php as a cron job.

2
  • i found my answer by @@returnthis.lau_ but i will appreciate anyone have other kind of process or approch for do this. Commented Feb 21, 2014 at 10:57
  • It's not a solution using PHP, but the Fat Controller will allow you to run instances of PHP scripts in parallel, handle if/when they fail and even run them as a daemon if you want. Simple to install and takes care of all the difficult stuff so you can concentrate on the business logic. Commented Feb 21, 2014 at 12:47

2 Answers 2

14

PHP DOES SUPPORT MULTI-THREADING

http://php.net/pthreads

Here is a multi-threaded example of the kind of logic you require:

<?php
define("SECOND", 1000000);
define("LOG",    Mutex::create());

/*
 * Log safely to stdout
 * @param string message the format string for log
 * @param ... args       the arguments for sprintf
 * @return void
 */
function slog($message, $args = []) {
    $args = func_get_args();
    if ((count($args) > 0) &&
        ($message = array_shift($args))) {
        $time = microtime(true);
        Mutex::lock(LOG);
        echo vsprintf(  
            "{$time}: {$message}\n", $args);
        Mutex::unlock(LOG);
    }
}

class MyTask extends Thread {
    public $id;
    public $done;

    public function __construct($id) {
        $this->id = $id;
        $this->done = false;
    }

    public function run() {
        slog("%s#%d entered ...", __CLASS__, $this->id);
        /* don't use sleep in threads */
        $this->synchronized(function(){
            /* simulate some work */
            $this->wait(10 * SECOND);
        });
        slog("%s#%d leaving ...", __CLASS__, $this->id);
        $this->done = true;
    }
}

$threads = [];

function get_next_id(&$threads) {
    foreach ($threads as $id => $thread) {
        if ($thread->done) {
            return $id;
        }
    }
    return count($threads);
}

do {
    slog("Main spawning ...");
    $id = get_next_id($threads);
    $threads[$id] = new MyTask($id);
    $threads[$id]->start();
    slog("Main sleeping ...");
    usleep(5 * SECOND); 
} while (1);
?>

This will spawn a new thread every 5 seconds, the threads take 10 seconds to execute.

You should try to find ways of increasing the speed of individual tasks, perhaps by sharing some common set of data.

Sign up to request clarification or add additional context in comments.

9 Comments

With the "small" caveat that pretty much nothing in PHP is thread safe. In his particular example, he probably cannot just require the file from multiple threads and hope it's going to work. He will have to add mutexes all over the place to get this working, and even then he will probably still have some random, hard to debug, race conditions.
The reason you're having to use words and phrases like probably, and pretty much, is because you don't actually know. Please read the document above.
Yes I don't actually know the code in the cron1.php file the OP mentioned, but for any non-trivial PHP code, I'm sure it's not as simple as launching multiple processes (which is what PHP was designed for). If the pthread module was completely free of any possible race condition that would be like magic, considering PHP was never designed for concurrent programming. The article you mentioned was written by the author of the module, who is not the most neutral person to discuss the pro and con of this approach.
The use of mutex in the example code provided only makes output readable, maybe I should have mentioned that. pthreads is a high level API, so safety is implicit, managed, for all operations. There is not enough room to well explain these things here, if this reads poorly I apologize. As the article mentions, it is not the module, in the sense of the PHP extension, that is not safe, but the underlying locale library that is not re-entrant (it manipulates process global state). This is not a common problem anymore, most libraries are re-entrant.
Lol.. Gotta love when people make assumptions about what they do not know and have not tested, and try to respond with "intelligent" insight into the issue.... Op: "I need this"... Answer: "It wont work"... Op: "Why not?"... Answer: "cause it probably wont, I don't understand it enough to answer why's and why nots, and its to hard, so it makes sense that its not possible"..
|
8

What you could do is run multiple processes at the same time, with something like this:

exec('php cron/cron1.php > /dev/null 2>&1 &');
exec('php cron/cron1.php > /dev/null 2>&1 &');
exec('php cron/cron1.php > /dev/null 2>&1 &');

Each exec call will run in the background so you can have as many as needed.

7 Comments

@SatishSharma, yes you could put the above, for example, in "maincron.php" then add this to the crontab - php /path/to/maincron.php
@returnthis.lau_ ok thanks i understand this. can you explain this command for me?
@N.B. have you another method? please provide it to me.
@returnthis.lau_ thanks a lot!!. i tried the your answer and its works perfectly for me. so +1 and accepted
actually on many shared hosts this can be the only solution and it's an efficient one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.