基于多线程套接字的服务器 - 在 php 中可能吗? [英] Multi threaded socket based server - possible in php?

查看:50
本文介绍了基于多线程套接字的服务器 - 在 php 中可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一堆用 php 编码的 cli cron 风格的脚本.

We have a bunch of cli cron style scripts that are coded in php.

其中一些服务使用 ftp 将数据发送到远程位置.

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.

问题是由于步骤 b 有相当多的开销(读取:减速),它首先必须连接到 ftp 服务器,登录,确保文件夹存在,如果不创建它,等等...我知道我知道,正确的做事方法是将这些转移合并为单次推送......但它比那复杂得多.我从这里简化了大约 30-40 个步骤.

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 ]

有了上面的内容,我们可以保持本地托管的基于套接字的服务器运行,并打开 ftp 连接,我们将跳过该过程中最长的部分,即 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.

虽然在 PHP 中为一次一个"样式系统进行设置相当简单,但我以前从未做过的事情是使其尽可能接近多线程.

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.

在哪里打开套接字(例如,127.0.0.1:10000),并且可以传入多个请求.如果需要,会生成子项",建立新的 ftp 连接等.

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.

任何人都可以深入了解如何在 php 中实现多线程,或者,是否还有其他更好的解决方案?Perl 是一种选择.自从我接触它已经有很多年了(YEARS ...),但我相信在一些好的文档面前花几天时间会让我加快速度以实现它.

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.

推荐答案

我们已经构建了一个系统,可以或多或少地满足您的需求.所以,绝对可以用 PHP 构建一个多进程应用程序.

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.

然而,这并非微不足道.如果您fork 一个子进程,您需要非常小心管理您的远程连接以避免出现问题.(使用 socket_* 系列函数而不是 fsockopen 更好的控制)

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)

此外,信号往往会中断您的正常程序流程.这当然是正常的,但 PHP 的构建并没有考虑到这一点 -> 为一些意想不到的结果做好准备.

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.

这篇关于基于多线程套接字的服务器 - 在 php 中可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆