Reconsider refactoring the InputHandler to use Tasks #80

Open
opened 3 months ago by kimimaru · 0 comments
Owner

Currently, each input sequence is processed on a dedicated thread. This means TRBot is capable of handling only a given number of input sequences at a time. On more powerful machines, this is a non-issue, but less capable machines could have problems keeping up on very active streams.

The second comment on this StackOverflow answer sums this up very well.

"One of the major advantages of async code is to allow one thread to work on multiple tasks at once, by avoiding blocking calls. This avoids the need for huge amounts of individual threads, and allows a threadpool to service many requests at once. However, given that async code usually runs on the threadpool, needlessly blocking a single thread with Thread.Sleep() consumes an entire thread that could otherwise be used elsewhere. If many tasks are run with Thread.Sleep(), there's a high chance of exhausting all threadpool threads and seriously hindering performance."

One of the challenges in the previous attempt at this was stopping all inputs. For some reason, Task.WaitAll was throwing exceptions occassionally. This may be prevented by implementing it differently this time around.

Currently, each input sequence is processed on a dedicated thread. This means TRBot is capable of handling only a given number of input sequences at a time. On more powerful machines, this is a non-issue, but less capable machines could have problems keeping up on very active streams. [The second comment on this StackOverflow answer](https://stackoverflow.com/questions/20082221/when-to-use-task-delay-when-to-use-thread-sleep/28413138#28413138) sums this up very well. ``` "One of the major advantages of async code is to allow one thread to work on multiple tasks at once, by avoiding blocking calls. This avoids the need for huge amounts of individual threads, and allows a threadpool to service many requests at once. However, given that async code usually runs on the threadpool, needlessly blocking a single thread with Thread.Sleep() consumes an entire thread that could otherwise be used elsewhere. If many tasks are run with Thread.Sleep(), there's a high chance of exhausting all threadpool threads and seriously hindering performance." ``` One of the challenges in the previous attempt at this was stopping all inputs. For some reason, `Task.WaitAll` was throwing exceptions occassionally. This may be prevented by implementing it differently this time around.
kimimaru added the
refactor
label 3 months ago
kimimaru added a new dependency 2 weeks ago
kimimaru added this to the TRBot 2.5 project 1 week ago
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date

No due date set.

Loading…
There is no content yet.