Socket based multithreaded server - possible in php?

We have a bunch of Cliron style scripts written in php.

Some of these services use ftp to send data to remote locations.

The way things work happens quite often:

a) Script start
b) Connect to ftp @ remote location
c) Send data
d) Close ftp connection
e) Terminate script
f) Return to A, repeat, within a short amount of time and send to the same target, but different data.

      

The problem is that step b introduces some additional overhead (read: slowdown) when it first has to connect to the ftp server, log in, make sure the folder exists if not created, etc. ... etc ... I know, I know, the right way to do something is to combine these translations into one push ... But it's much more complicated than that. I have simplified about 30-40 steps from here.

So I was hoping to create a system like this:

[ CRON CLI SCRIPT ] --->

[ LOCALLY HOSTED SOCKET BASED SERVER THAT KEEPS THE FTP CONNECTIONS OPEN ] --->

[ REMOTE FTP ]

      

With that said, we can keep a local socket based server running and open ftp connections and we'll skip the longest part of the process - the items related to ftp authentication.

While setting up PHP for a one-at-a-time style system in PHP is pretty trivial, I've never made it as close as possible to multithreading before.

This opens a socket (eg 127.0.0.1:10000) and may receive multiple requests. Descendants are created as needed, new ftp connections are established, and so on. Etc.

Can anyone shed some light on how to do multithreading in php, OR if there is another better solution? Perl is an option. It has been many years (YEARS ...) since I touched it, but I'm sure a couple of days before some good paperwork will get me fast enough for this to happen.

+1


source to share


2 answers


We have created a system that does more or less of what you want. Thus, it is possible to create a multi-process application in PHP.

This, however, is not trivial. If you fork from a child process, you need to manage your remote connections very carefully to avoid problems. (use socket_ * family of functions instead of fsockopen for better control)



Also, signals tend to interrupt your normal program flow. This is not normal, but PHP was not built for this purpose -> be prepared for unexpected results.

+3


source


try a custom gear, you can handle the most expensive cpu usage with a gear, gearman make a new thread for each process.



0


source







All Articles