[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: network time service, non-blocking?
From: |
arnold |
Subject: |
Re: network time service, non-blocking? |
Date: |
Wed, 11 May 2022 14:21:20 -0600 |
User-agent: |
Heirloom mailx 12.5 7/5/10 |
Petr,
Gawk's network support is minimal. In the long run you may
be better off in Python or Go.
Arnold
david kerns <david.t.kerns@gmail.com> wrote:
> On Wed, May 11, 2022 at 1:39 AM Petr Slansky <slansky@gmail.com> wrote:
>
> > Hello Gawk,
> >
> > 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.
> >
> > Petr
> >
>
> 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