简体   繁体   中英

Async server in hacklang

I'm trying to create async server in hacklang. File name is 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:

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:20:39,47: This is why I think it is Awaitable

But I don't want to block and wait for handleClient.

Then I run code this way: 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.

Do I do something wrong? Is it possible to create server like this in hacklang at all?

$ 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.

Well, if you don't await them, then there's no guarantee that they will run! Hack's async functionality lets you run different data fetching asynchronously, but you need to actually get the result out somehow. As you've written this code, HHVM makes no promises about how much of handleClient is going to run. 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.

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. Keep to PHP's one-request-one-thread model for that.

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

Don't turn off the typechecker -- it was indicating that something was wrong with your code from the get-go. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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