RFC: asyncdispatch.poll behaviour #5155

Closed
vegansk opened this Issue Dec 28, 2016 · 9 comments

Projects

None yet

3 participants

@vegansk
Contributor
vegansk commented Dec 28, 2016 edited

It's hard to discuss this in gitter due to timezone differences, so I made the issue.

The problem. I'm creating an actors library inspired by akka actors. And I want to:

  1. allow async actions in actor handlers
  2. use asyncdispatch.sleepAsync for deferred messages

I thought that I can use asyncdispatch like that: https://github.com/vegansk/nimactors/blob/489b07f1728cb5993ac9ef924c3899aee01bac7e/src/nimactors.nim#L72 , (example), but as it was explained here, I can't use poll without any pending callback, timers, handlers, it throws the exception.

So, can I implement the foresaid requirements using asyncdispatch? I can do it if it will not throw the exception if there is no pending operations. And I guess that it doesn't affect another async applications.

@vegansk
Contributor
vegansk commented Dec 28, 2016 edited

By the way, if we have only timers and callbacks, poll will eat 100% cpu, isn't it?

@dom96
Member
dom96 commented Dec 28, 2016 edited

Quick thought: wouldn't moving the poll() after line 92 ensure that there is always at least one timer, callback or handle in the dispatcher?

@vegansk
Contributor
vegansk commented Dec 28, 2016

@dom96, no. Async operations could be made in handler (lline 83) without using deferred messages

@dom96
Member
dom96 commented Dec 28, 2016

@vegansk I'm not sure I understand. You can call async operations before calling poll.

@vegansk
Contributor
vegansk commented Dec 28, 2016

I need the loop that must handle messages that comes from another thread with the function defined somewhere. I can't call async operations untill some message will be received. I can't count that handler will call any async operation at all. It's up to the actor's creator

@cheatfate
Contributor

The biggest problem of your code is dependency on channels and async at the same time, because you can't wait for message from channels in asynchronous way. To resolve this i'm using pipes, you can use my implementation of asyncpipe.nim, or make your own implementation, but this of course will complicate your implementation (because you will need to make dispatcher for every thread and there will be some problems with automatic register/unregsiter descriptors with async queue)

@vegansk
Contributor
vegansk commented Dec 29, 2016

@cheatfate, I don't want to use IPC for in-process communications. I think, I found the solution that can satisfy all: #5163

@cheatfate
Contributor

@vegansk to fully satisfy asynchronous behavior in threaded environment, you need to use it. Please check LWAN lightweight asynchronous multi-threaded event-based web server source code.
Also my wanted webserver POC uses same technique, and it outperforms asynchttpserver by 3x times on multi-core CPUs.

@vegansk
Contributor
vegansk commented Dec 29, 2016

@cheatfate, thanks, i'll try it, but later, for now the project must be released

@dom96 dom96 closed this in 8e71949 Dec 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment