yglukhov / asyncthreadpool

Awaitable threadpool for nim
MIT License
48 stars 2 forks source link

can't inferre task return type. #8

Closed bung87 closed 2 years ago

bung87 commented 3 years ago

cmd: nim c -d:ChronosAsync --threads:on tests/tstreamserver.nim

https://github.com/bung87/scorper/blob/36aa15fc662718c07065306c0c95b735763c47f1/src/scorper/http/streamserver.nim#L858

when MultiThreads:
      let retry = await req.server.pool.spawn processRequest(req)

Error: type mismatch: got <RetTypegensym817>`

Clonkk commented 3 years ago

This is caused by your proc being async.

See https://github.com/yglukhov/asyncthreadpool/issues/5

Use non-async proc for expected results

bung87 commented 3 years ago

I now cast types, but still asyncthreadpool not work as I expected.

Clonkk commented 3 years ago

I don't understand what value you want to cast.

The issue comes from the compiler handling return type of async proc differently.

Change processRequest definition and remove the async and it should be fine :

proc processRequest(
  req: Request,
): Future[bool] {.async.} =

The underlying question (and perhaps I shouldn't have closed my issue) is should a threadpool be able to handle async proc ? It's not obvious that it should but you'd want @yglukhov thoughts on the matter.

bung87 commented 3 years ago

the readme clam use await for async proc , while the return type cant inferred , in this case assign the returns to variable a , when I call if a, if will trigger type doesn't match, so I casting it, I still get copyAux error when channel send back , like I did tried use threads and channels without asyncthreadpool. no magic lib just like go's go routine that I only need call go some proc.

yglukhov commented 3 years ago

The spawned proc has to be synchronous. Whatever it returns is then wrapped into future by the threadpool.

Clonkk commented 3 years ago

Note: in async functions you should use await instead of waitFor.

I think this sentence in the Readme is what @bung87 is referring to.

It should be clarified whether or not async works with thread pool

yglukhov commented 3 years ago

Maybe the readme is confusing, let me explain.

Does that make sense?

bung87 commented 3 years ago

much clear, thanks!

yglukhov commented 2 years ago

Feel free to add to the readme, if needed