简体   繁体   English

Hacklang中的异步服务器

[英]Async server in hacklang

I'm trying to create async server in hacklang. 我正在尝试在hacklang中创建异步服务器。 File name is first.php: 文件名是first.php:

<?hh

namespace MyExperiment;

async function server(string $host, int $port): Awaitable<void> {
    $master = stream_socket_server("tcp://$host:$port");
    stream_set_blocking($master, 0);

    while (true) {
        await stream_await($master, STREAM_AWAIT_READ, 1.0);

        $clientSocket = stream_socket_accept($master);
        stream_set_blocking($clientSocket, 0);

        handleClient($clientSocket);
    }
}

async function handleClient($socket): Awaitable<void> {
    await stream_await($socket, STREAM_AWAIT_READ, 1.0);

    $data = fread($socket, 1024);
    echo $data;

    await stream_await($socket, STREAM_AWAIT_WRITE, 1.0);

    fwrite($socket, 'aaaaaaaa');

    fclose($socket);
}

function run(): void {
    \HH\Asio\join(server('192.168.0.97', 8080));
}

run();

But this does not work. 但这是行不通的。 hh_client on this code says: 此代码上的hh_client说:

first.php:16:3,29: This expression is of type Awaitable, but it's either being discarded or used in a dangerous way before being awaited (Typing[4015]) first.php:16:3,29:此表达式的类型为Awaitable,但是在被等待之前它已经被丢弃或以危险的方式使用(Typing [4015])

first.php:20:39,47: This is why I think it is Awaitable first.php:20:39,47:这就是为什么我认为它是可以等待的

But I don't want to block and wait for handleClient. 但是我不想阻止并等待handleClient。

Then I run code this way: hhvm -d hhvm.hack.lang.auto_typecheck=0 first.php 然后我以这种方式运行代码:hhvm -d hhvm.hack.lang.auto_typecheck = 0 first.php

Server starts. 服务器启动。 But when I start to send requests to server http://192.168.0.97:8080/ from browser server blocks after few requests for very long time and does not accept new connections anymore. 但是,当我在很长一段时间内几次请求后开始从浏览器服务器块向服务器http://192.168.0.97:8080/发送请求时,不再接受新的连接。

Do I do something wrong? 我做错什么了吗? Is it possible to create server like this in hacklang at all? 是否有可能在hacklang中创建这样的服务器?

$ hhvm --version
HipHop VM 3.11.0 (rel)
Compiler: tags/HHVM-3.11.0-0-g3dd564a8cde23e3205a29720d3435c771274085e
Repo schema: 52047bdda550f21c2ec2fcc295e0e6d02407be51

But I don't want to block and wait for handleClient. 但是我不想阻止并等待handleClient。

Well, if you don't await them, then there's no guarantee that they will run! 好吧,如果您不await它们,则无法保证它们会运行! Hack's async functionality lets you run different data fetching asynchronously, but you need to actually get the result out somehow. Hack的异步功能使您可以异步运行不同的数据获取,但是您实际上需要以某种方式获得结果。 As you've written this code, HHVM makes no promises about how much of handleClient is going to run. 在编写此代码后,HHVM 无法保证将运行多少handleClient It might be all of it, it might be none of it, it might suspend handleClient at some random await statement in the middle of it. 可能是全部,也可能全都不是,它可能会在中间的某个随机await语句中挂起handleClient

It's unclear what exactly you are trying to do here, but async might not be the right facility for it. 目前尚不清楚您到底想在这里做什么,但是异步可能不是正确的工具。 Hack's async is not multithreading , and it looks like you might be trying to use it that way. Hack的异步不是多线程的 ,看起来您可能正在尝试以这种方式使用它。 Keep to PHP's one-request-one-thread model for that. 为此,请遵循PHP的“一请求一线程”模型。

I strongly recommend you read the (recently rewritten) official documentation of async which explains what it is good for and how to use it. 我强烈建议您阅读(最近重写) 异步的正式文档,文档解释了异步的好处以及使用方法。

Then I run code this way: hhvm -d hhvm.hack.lang.auto_typecheck=0 first.php 然后我以这种方式运行代码:hhvm -d hhvm.hack.lang.auto_typecheck = 0 first.php

Don't turn off the typechecker -- it was indicating that something was wrong with your code from the get-go. 不要关闭类型检查器-这表明一开始的代码有问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM