help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: network time service, non-blocking?


From: Neil R. Ormos
Subject: Re: network time service, non-blocking?
Date: Wed, 11 May 2022 10:36:06 -0500 (CDT)

david kerns wrote:
> Petr Slansky <slansky@gmail.com> wrote:

>> this is not real bug report, it is something
>> like a feedback. I use gawk 4.1.4.

>> I tried to use gawk for some network service,
>> to report progress of data processed from
>> pipeline to websocket but I think I cannot do
>> that because gawk doesn't support non-blocking
>> sockets. To illustrate that, I use example with
>> simple time service, similar to

>> https://www.gnu.org/software/gawk/manual/gawkinet/html_node/Setting-Up.html#Setting-Up

>> $ cat timesrv.awk
>> # Time server
>> BEGIN {
>>   service = "/inet/tcp/8888/0/0";
>>   while (1) {
>>      time = strftime();
>>      print time;
>>      print time |& service;
>>      close(service);
>>   }
>> }

>> # run it
>> $ gawk -f timeserv.awk

>> # other terminal
>> $ sleep 45; date; curl http://localhost:8888

>> The problem of this server is that it reports
>> "old" time. It samples time to a variable and
>> it waits for an incoming connection from a
>> client, then it reports time and samples new
>> time. When clients don't connect frequently,
>> they get wrong (old) time.

>> Is there a way, to check if client connected to
>> listening socket? Open server socket, do some
>> other tasks and from time to time check if
>> client is connected; when client is connected,
>> serve fresh data otherwise continue to process
>> data. It is possible that I cannot use gawk for
>> my task and I have to use Python, Go or other
>> tool.

> you should be using the awk-help mailing list
> for this question ...

> since you are using curl/http to connect (ie
> have lots of incoming data to indicate a new
> connection) just make the blocking read at the
> top of the loop:

> # Time server
> BEGIN {
>   service = "/inet/tcp/8888/0/0";
>   while (1) {
>      service |& getline; # wait for new request
>      time = strftime();
>      print time;
>      print time |& service;
>      close(service);
>   }
> }

> this code still only returns the time, (in a non
> machine friendly format) and not a valid http
> reply, but that's beyond the scope of the OP

Although the tread was originally posted to the bug-gawk list, I'm replying on 
the help-gawk list because at the moment, that's where discussion seems most 
appropriate, but maybe the discussion will turn into a feature request for true 
non-blocking I/O or timeouts that apply to more I/O activity than reads.

Reading between the lines, I think the OP's sample code that sends a string 
returned by strftime() is just a conjured example of sending some time-varying 
content and is intended as a placeholder for some other processing that would 
produce time-varying data to be sent to the client.

The OP says he intends that when the client is not connected, he wants to 
continue to process data.

The suggestion of doing a blocking read at the top of the loop seems to defeat 
the intent of continuing to process data while the client is not connected.

One might think setting a read timeout (see manual, Section 4.11) could solve 
the problem, but it doesn't, I suppose because gawk is blocking on connect() 
(or something like it), rather than the actual read.  The manual alludes to 
this by warning that even with a read timeout set, Gawk can still block.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]