Hello Nim world,

I'm doing mostly IO related processing in various threads and I want them to be 
able to communicate in an asynchronous way.

An example scenario would be:

  * main thread is asynchronously processing incoming HTTP requests, sends off 
work (a message) to a worker thread and answers the HTTP client when it got the 
response from the worker thread
  * worker thread is waiting for work from the main thread an depending on the 
message it calls an API via HTTP to let the work be done
  * main thread should be able to accept more incoming HTTP requests with new 
work
  * worker thread should continue accepting new work, while waiting for the 
work to be done



In the optimal scenario both threads would be asynchronous and just pass 
messages and along and process the corresponding (evented) IO. In between we 
also have to deal with disconnects, which should be no problem with exceptions.

Now the thing is - channel handling isn't asynchronous. I can do a blocking 
`send` and `recv`, which I don't want to.

I can simulate asynchronous with `trySend` and `tryRecv`, but this somehow 
doesn't feel right.

If I do it this way, I have the problem that `sleepAsync` can introduce a 
latency up to 500ms, which introduces too much response delay for the main 
threads' clients. But I have to do some sleeping, otherwise - if I understand 
this right - the IO events aren't processed. So I have to use a loop with 
`trySend` / `tryRecv` until there are messages to process with blocking `send` 
and `recv`, and in between doing `cpuRelax` to get the pending IO events 
processed and prevent the CPU from doing a total busy loop.

Are these assumptions correct? Am I missing something elementary here?

Thanks in advance,

SID

Reply via email to