xangr
xangr

Reputation: 879

Async/Thread on PHP7 with FPM

I found that pthreads does not work on web environment. I use PHP7.1 on FPM on Linux Debian which i also use Symfony 3.2. All I want to do is, for example:

  1. User made a request and PUT a file (which is 1GB)
  2. PHP Server receives the file and process it.
  3. Immediately return true to user (jsonResponse) without awaiting processing uploaded file
  4. Later, when processing file is finished (move, copy, duplicate whatever you want) just add an event or do callback from background and notify user.

Now. For this I created Console Command. I execute a Process('bin/console my:command')->start(); from background and I do my processing. But this is killing a fly with bazooka for me. I have to pass many variables to this executable command.

All I want to is creating another thread and just return to user without awaiting processing.

You may say this is duplicate. And point to pthreads. But pthreads stated that it is only intended for CLI. Also last version of pthreads doesn't work with symfony. (fatal error).

I am stuck at this point and have doubt if should I stay with creating processes for each uploaded file or move to python -> django

Upvotes: 3

Views: 2885

Answers (1)

Gordon
Gordon

Reputation: 316939

You don't want threads. You want a job queue. Have a look at Gearman or similar things.

Gearman provides a generic application framework to farm out work to other machines or processes that are better suited to do the work. It allows you to do work in parallel, to load balance processing, and to call functions between languages. It can be used in a variety of applications, from high-availability web sites to the transport of database replication events. In other words, it is the nervous system for how distributed processing communicates.

Upvotes: 3

Related Questions