想把workerman改成WINDOWS服务程序可行吗?方案如下。

tmig

workerman在WINDOWS下用命令行启动,有点不方便。想改成WINDOWS服务程序方式运行.
想法如下:
1,下载PHP的扩展:php_win32service.dll
2,在Select.php中修改:

public function loop()
{
    $e = null;
    while (1)
    {
        ....
    }
}

改成:

public function loop()
{
    $e = null;
    while (1) {
        if (WIN32_SERVICE_CONTROL_STOP == win32_get_last_control_message()) {
            die("服务已停止!");
        }

    }
}

不知道可行不?如果可行对性能有影响没?

3892 5 0
5个回答

walkor

赞!
性能影响应该不大。你可以写个脚本单独压测下
WIN32_SERVICE_CONTROL_STOP == win32_get_last_control_message()
试下,看每秒能运行多少次。
另外=== 比 == 性能要高一些

  • 暂无评论
tmig

查看PHP官方文档:

//检测服务是否存在
if (!win32_start_service_ctrl_dispatcher( _SERVICENAME )) {
 die("没有发现正在运行的  服务");
}
win32_set_service_status(WIN32_SERVICE_RUNNING);

//如果运行中
while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) 
{
 # do your work here.
 # try not to take up more than 30 seconds before going around the loop
 # again
}

知道这个while要写到哪里去!Select.php文件在的loop中修改通不过!
版主帮帮写下!

  • 暂无评论
tmig

官方的例子:

<?php

class TestClass {
    private $args;

    function __construct($arg) {
        $this->args = $arg;
        $this->run();
    }

    private function run() {
        if ($this->args == 'run') {
            win32_start_service_ctrl_dispatcher('test_service');

            while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) {
                # do your work here.
                # try not to take up more than 30 seconds before going around the loop
                # again
            }
        }
    }
}

$object = new TestClass($argv);

?>
  • 暂无评论
tmig

问题决解了!
写了个空壳的服务程序,在里面直接执行php.exe start.php
停止服务时!KILL掉PHP.EXE的进程。
已稳定运行两周了。
再为不为在WINDOWS平台下要登陆系统在命令行运行的烦恼!
[attach]172[/attach]

  • 暂无评论
walkor

  • 暂无评论
年代过于久远,无法发表回答
🔝