3

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

xangr
  • 879
  • 14
  • 28
  • 3
    You don't want threads. You want a job queue. Have a look at Gearman or similar things. – Gordon Dec 28 '16 at 16:20
  • I really liked it. I looked to doc. and i think this will suits my need better. I also mentioned **queue** in my question but i removed later. After seeing Gearman and queue mechanism, I decided to go with job queue. Also, read that making -for example- 2 threads per execution and lets say 400 client comes to server it will run 800 threads. which is its not what i want either. Gearman is the key here. I will deep dive into it. Thanks. Oh! You may add this as answer :) – xangr Dec 28 '16 at 16:25
  • 1
    Maybe you are looking for async processing: https://gist.github.com/LeonanCarvalho/62c6fe0b62db8a478f502f84c5734c83 – LeonanCarvalho Jan 31 '18 at 22:56
  • @LeonanCarvalho, I handle most of those jobs on background tasks such as Sidekiq or Gearman. But I do like async classes you provided. May I download and use them? – xangr Feb 01 '18 at 12:25
  • 1
    @xangr sure, use as you wish. Just pay attention to singleton instances among forked process, all singleton and socket/net connections (such DB connections) must be recreated in the forked process. – LeonanCarvalho Feb 01 '18 at 13:07

1 Answers1

3

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    Plus, https://github.com/mmoreram/GearmanBundle for who uses Symfony like me. It's getting better every minute :) – xangr Dec 28 '16 at 17:00