1

Is it possible to multiprocess/multithread in php from a http request.

Background

I am working on a project where I expect some data (not from a user so data upload is consistent) to be uploaded to an api.

I want to decouple the process such that if data upload is successful, I notify this client and disconnect it and process the data in another thread. Right now, the client waits for the data to be processed and on success is disconnected.

My research

I have looked into forking a process but it always came with a caveat of not using it over a web server and additionally I wanted a package/module that can scale with the project I am working on.

Pthreads: This is the php threading module. As noted above, it is not configured to work with a web server only on cli.

amphp: this is a module built with pthreads and it comes with the same caveat.

This prompts the question how best can one solve the problem? I understand the challenge, if you open a new thread for each connection you will have a 1000 threads for a 1000 connections and can easily overload the server if this increases but my case is unique in that I do not wish to multi thread for human connections and the number of connections can be easily anticipated.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92
J.Ewa
  • 205
  • 3
  • 14
  • Perhaps look into using a message queue (I've used RabbitMQ) it allows much more flexibility and resilience. – Nigel Ren Oct 05 '18 at 16:12
  • Well thanks. I am seriously looking into that right now. It seems there is paucity of info with regards to opening a thread in php from http requests. – J.Ewa Oct 08 '18 at 07:19
  • 1
    Amp isn't based on pthreads, just a minor component (`amphp/parallel`) might use pthreads if it's available, but it also runs fine without pthreads. – kelunik Nov 03 '18 at 10:20

1 Answers1

0

Ok for anyone that has similar doubts like me, I think this can be fixed with a message broker. I thought of different possibilities like a daemon, a crontab but I think a message broker is the cleanest way of doing this.

J.Ewa
  • 205
  • 3
  • 14