We have a bunch of cli cron style scripts that are coded in php.
A few of these services use ftp to send data to remote locations.
The way things are set up, what happens quite frequently is:
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 issue is that there is quite a bit of overhead (read: slowdown) due to step b, where it first has to connect to the ftp server, login, make sure the folder exists, if not create it, etc etc... I know I know, the right way to do things would be to consolidate these transfers into single pushes... But its far more complicated then that. I simplified about 30-40 steps from here.
So what I was hoping on doing is setting up a system like this:
[ CRON CLI SCRIPT ] --->
[ LOCALLY HOSTED SOCKET BASED SERVER THAT KEEPS THE FTP CONNECTIONS OPEN ] --->
[ REMOTE FTP ]
With the above, we can keep the locally hosted socket based server running, and the ftp connections open and we would skip what is the longest part of the process, the ftp authentication related items.
While setting this up for a 'one at a time' style system in PHP is fairly trivial, what I have never done before is making this as close to multi threaded as possible.
Where by, the socket is opened (for example, 127.0.0.1:10000), and multiple requests can come in. If needed, 'children' are spawned, new ftp connections made, etc etc.
Can anyone shed some insight into making this multi threaded in php, OR, if there is another better solution out there? Perl is an option. Its been years (YEARS...) since I have touched it, but I am sure a couple days in front of some good docs would bring me up to speed enough to make it happen.
We have build a system that does more or less what you want. So, it is definitely possible to build a multi-process application in PHP.
It is however, not trivial. If you fork off a child process, you need to very carefully manage your remote connections in order to avoid problems. (use the socket_* family of functions instead of fsockopen for better control)
Also, Signals, tend to interrupt your normal program flow. This is off course normal, but PHP was not build with this in mind -> be prepared for some unexpected results.
try to user gearman , you can handle most expensive cpu usage with gearman , gearman make a new thread for each process .
精彩评论