문제

I have a gearman server, that has a php worker-script in background (managed by supervisord). The process is asynchronous, so the main script does not have to wait.

The problem is that I do not need the worker 100% of the time in background, I only need it when there is a job for the worker. Is there any way to avoid background php process? e.g. some script that will monitor incoming tasks and execute appropriate php files?

The solution I have in mind is to create a shell scipt that will execute php code, something like:

gearman -w -f 'my_func' ./my_func_exec.sh 

where my_func_exec.sh:

php -q my_func.php

But how should I pass workload to my_func.php? Or are there ready-made solutions?

도움이 되었습니까?

해결책

First of all, I think you shouldn't worry about the background worker: The overhead of it idling is close to zero, but the overhead of it starting and stopping is not.

You will also run into lots of locking troubles, to avoid concurrent client requests starting up more than one

That said, here is a pattern for your use case:

  • Define a flagfile and touch it on startup (this is important: File existing means worker not running)
  • A client, that wants to consume your worker, unlink()s it, keeping the return value
  • If this return value is true, then this thread was the one to unlink the file, and has sort of acquired a lock on worker creation: So he creates a worker via shell_exec() - be carefull to start it in the background (i.e. /path/to/php php -q /path/to/my_func.php &)
  • The client waits for the worker to become available and submits the job
  • The worker does not use the normal while($worker->work()); loop, but a single call, then writes the flagfile and ends.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top