Wrapping synchronous code into asynchronous call

2020-10-05 Thread mrhdias
After some investigation I found a way to make it work! But I don't know if it is the best solution, or if it is correct. I write a small program to illustrate the idea. import asynchttpserver, asyncdispatch import os import threadpool import random import strutils

Wrapping synchronous code into asynchronous call

2020-10-05 Thread cblake
The usual way to build an async/non-blocking interface to synchronous/blocking system calls like `read(hard disk)` is a thread (or kid _process_ or even unrelated (and likely multi-OS-threaded) "server" process) that does the invoking of the possibly blocking system calls. This "helper entity" t

Wrapping synchronous code into asynchronous call

2020-10-05 Thread dom96
> This is a test to simulate the use of a synchronous function in an > asynchronous environment! I feel like you're trying to say something by this but I am missing it. I know that this what you are doing, your `testSync` proc can use `sleep` since it's spawned in another thread, but you cannot

Wrapping synchronous code into asynchronous call

2020-10-05 Thread dom96
> There aren’t any async-friendly ways to wait/notify? Could you open a pipe, > create an asyncsocket on it, and then read a byte from it? That's true, you could do this but as @xendi noted you'd need to run the event loop on each thread you're using. Not a bad idea to be fair, might be good to

Wrapping synchronous code into asynchronous call

2020-10-04 Thread xendi
Maybe there's a helper for reading bytes as you propose but don't forget, there must always be an event loop somewhere beneath, right?

Wrapping synchronous code into asynchronous call

2020-10-04 Thread snej
> The only thing you can do is spawn a new thread and then use a rather > inefficient busy loop to poll its completion. There aren’t any async-friendly ways to wait/notify? Could you open a pipe, create an asyncsocket on it, and then read a byte from it?

Wrapping synchronous code into asynchronous call

2020-10-04 Thread mrhdias
This is a test to simulate the use of a synchronous function in an asynchronous environment! What should I do to run the synchronous sleep function at the same time as the asynchronous sleep function without blocking?

Wrapping synchronous code into asynchronous call

2020-10-04 Thread dom96
I mean, did you check that `testAsync` definitely doesn't get called? Like did you add an echo at the beginning of it? Anyway, I bet the problem you're seeing is because you're blocking the thread in `test` by calling `sleep`, that will block all of async. Never call `sleep` in an async proc, i

Wrapping synchronous code into asynchronous call

2020-10-04 Thread mrhdias
Thanks @dom96 for the reply. I wrote this code to test your solution: import asyncdispatch import os import threadpool proc testSync(): bool = var count = 0 while true: sleep(1000) echo "wakeup sync... ", $count count += 1

Wrapping synchronous code into asynchronous call

2020-10-04 Thread dom96
The only thing you can do is spawn a new thread and then use a rather inefficient busy loop to poll its completion. This is how I did stdin reading in Chapter 3 of Nim in Action, you can use it as an example:

Wrapping synchronous code into asynchronous call

2020-10-04 Thread mrhdias
My question is how to make [db_mysql](https://nim-lang.org/docs/db_mysql.html) module work asynchronously? I know, that there are other mysql implementations, but this module is part of the standard library and is simple to use. But, there Is any way to call any synchronous function asynchronou