The Crystal Programming Language Forum

How to sync fibers with IO, flush IO?

I have several fibers running in crystal 0.34, when I write some lines to a socket, the data is not reliable sent out, unless I send more data in a loop or put a small sleep in my send() method. Is there a better way to do this ?
Sync did not help me for the socket.
https://crystal-lang.org/api/0.34.0/IO/Buffered.html#sync=(sync)-instance-method

def send (object)
  @client << object + "\n"  #send to TCP_SERVER
  sleep 0.000001  # this works reliable, output is flushed and sent to server, but why ?
  #@client.flush         # does not work for me  
  #Fiber.yield             # does not work for me
end

Any reproducible code?



Can you run that without Gambas3 running?

What I meant with “reproducible code” is the smallest snippet that exhibits that behavior, without any external dependencies if possible. If flush or sync isn’t working as expected for Socket, you should be able to reproduce it with much simpler programs.

You do not need Gambas3. Socat is good enough for the server side !
rlwrap socat tcp-l:9090 -
After some searching I found the problem on Gambas GUI server side.
There I get one receive event when several lines arrive, adding a loop till EOF solved this.

While Not Last.eof
  Line Input #Last, sRead         'read line by line works better for this use case
  Print "Received data <--" & sRead & "\n";
  If Len(sRead) > 0 Then setobject(sRead)
Wend