0

I used to use exec() function to trigger new php threads (And don't need the output of these php scripts), something like

for ($i=0;$i<5;$i++) {
exec('/usr/bin/php script' . $i . '.php > /dev/null 2>&1 &');
sleep(1);
}

I'd like to ask if there is any alternative native PHP function to do the same job, since my web hosting provider does not enable exec and i'm not willing to switch to another one.

Thank you very much.

aye
  • 301
  • 2
  • 16
  • `exec()` does not "trigger php threads". It starts completely separate php interpreter processes, a very expensive task. – arkascha Aug 20 '13 at 06:53
  • Actually i'm not familiar with the correct terminology, but that code serve my purpose (for example, script1.php, script2.php ... are script that need to be run until finished, but i just don't need to display the output, also the task in these script takes time (some seconds), but not CPU hunger) – aye Aug 20 '13 at 06:57
  • Please, don't try your hand at multi-threaded programming just yet. [Threads are evil](http://www.eecs.berkeley.edu/Pubs/TechRpts/2006/EECS-2006-1.pdf), albeit a necessary evil. PHP wasn't (nor should it ever be) intended to be used for concurrent computing. It's just not the right tool for the job. You can ask the system to spawn a child process, and yes, you can choose _not_ to wait for the child's response, but just step back, and ask yourself this: _"Isn't there another way? Do I really need threads/child processes?"_ Often, the answer is no... – Elias Van Ootegem Aug 20 '13 at 07:01
  • is the exepmple script integrated in a large, complex php file? if not, can you use a different programming language to run your php scripts? like bash? or python? – jnhghy - Alexandru Jantea Aug 20 '13 at 09:40
  • The parent php script is very simple. It includes several `explode` and `implode` calls and a loop to use `exec` function. Maybe using the other language is the only way to solve this problem. I see there are some php packages that can generate new php script, but i don't have much time to learn how to use it for the time being. Thank you very much for your suggestion – aye Aug 20 '13 at 10:54

1 Answers1

0

Typically such tasks are spawned via internal requests inside the http server. Just use phps [virtual()][1]or the [curl extension][2] for more complex tasks.

You are however limited to phps web configuration, so take care about the limits like execution timeout and the like. I doubt there is a way to spawn a real process on a web hosters system. That would be a horrible security thread!

Often such tasks are also delegated to some sort of job scheduler implementation which is triggered by cron, poor mans cron or an ajax based trigger. This means you have to implement a scheduler for jobs waiting to be processed. But it also brings a good separation of separate jobs.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • So, in short, there is no alternative function for me to do the same job? I don't want to run these scripts sequestially (by `include()`) because it will take long time. Each script will consume about 3-5 seconds, so there will not be any problem with the hosting limits. – aye Aug 20 '13 at 07:58
  • Most likely there is no way to spawn another process, but what I sketched above offers several alternative approaches. Why are they not suitable? – arkascha Aug 20 '13 at 08:00
  • Because the server limit the number of cron job per hour, so cron job is not a solution for this specific problem. Unfortunately i'm also not familiar with ajax. Right now i'm going to recode the main script to use python as suggested by alex alex. Thank you for your help :) – aye Aug 20 '13 at 10:57