StefanSalewski / NimProgrammingBook

Computer Programming with the Nim Programming Language -- A gentle Introduction
259 stars 19 forks source link

Some thoughts about async #21

Open geohuz opened 1 year ago

geohuz commented 1 year ago

I have serveral concerns about async usage:

  1. I saw some where on the internet that if we want to check future is failed then we should use yield instead of await to execute the async procedure. Would you put some words in the asyn execute section?
  2. The {.multisync.} pragma, a simple example should be good for this pragma
  3. About the {.async.}, Future[T] on the signature of procedure, I find it's quite confusing sometimes, the example in the book as below:
    proc tick(t: string): Future[void]  {.async.} =

The async pragma can be removed without any problem. So my current understanding is: await keyword can only be used inside a proc which has async pragma, and we can use yield to replace await if we want to check the future status, and we use await/waitFor/asyncCheck on any procedure with return type Future[T].

I finally found the description in the standard library is more accurate and helpful: https://nim-lang.org/docs/asyncdispatch.html

the only thing I don't quite understand is the description in the discard future: Note however that this does not wait for completion, and you should use waitFor for that purpose. I don't know for what purpose I should use waitFor

StefanSalewski commented 1 year ago

Thanks for your question. Unfortunately, async is a difficult topic, and one for which I am really no expert. Personally I have never needed or used async for my own projects. And when needing ansyc, I would try to use parallel threads instead, or when that does not work well, I would try the chronos module from Status Corporation. For some time, I had the hope that Disrupteks CPS could replace async. As my book is a book mostly for beginners, I initially was going to leave out async and macros completely, or was hoping that some other expert would support me for that two topics. But as I got no support, and because I felt that the book is not really complete without these two topics, I tried to read all available stuff about it, and then wrote these sections. I think they are not too bad, but actually they are not written by an insider or expert, sorry. I have to admit that this is not really a satisfying answer. I would be very nice from your side if you could ask such advanced questions elsewhere, like Nim forum, StackOverflow or Reddit. As you may know, Nims current async was created by Mr. Picheta, and most of which I know about async is from his book and from some other documentation. Later Status Corporation created the Chronos replacement, see https://github.com/status-im/nim-chronos. If you should be not able to get support elsewhere, then I would have to investigate your issues, but that may take some time then.

My whole motivation of using Nim is not that great currently: All the years I had the feeling that the Nim leaders, Rumpf, Picheta and Miran are often very unfriendly to some people including myself, and recent posts like

https://forum.nim-lang.org/t/9753#64224 https://forum.nim-lang.org/t/9745#64238

only confirm that. In the last 8 years I put a lot of unpaid work into the Nim project. I still like the language, but my feeling is, that Nim has been srinking over the last 5 years, and my feeling is, that rude behaviour of leaders are the main reason for that. You may know, that they banned or drived out many nice and bright people, Krux, Disruptek, Timothe, and some others. I have seen so many bright and friendly people over the years in the Nim community, but nearly most of them disappeared. So the fun is gone.

Let me know if you still need support.

geohuz commented 1 year ago

Yeah, I have the same feeling as you for the Nim, I love the language on it's flexible/good perfermance/small footprint etc, and I've used it in my several projects as backend services, I'm quite satisifed with them overall. But I always find hard to get deep knowledge on the language. I'm a guy prefer "pay only for what you eat" style of doing things, so for asyncdispatch part, there are several of my projects needs to cooperate several client connections in a single app so I have to eat the async food. Especially a requirement to connect postgresql notification service and act on notifiction in a long running app with other tasks, but none of the current implmentation of the pg related nim package does this, so I have to roll my own solution. During the time of learning I found it's hard to get help from forum and lib author, the github repository seems abandoned and issue doesnt get fixed. I really hope the language has wide spread adoption so more people like you can push it forward!

geohuz commented 1 year ago

Hey the discussion in the https://forum.nim-lang.org/t/9791 looks promising ;-)