davidMcneil / rants

An async NATS client library for the Rust programming language.
Apache License 2.0
81 stars 11 forks source link

task::block_on(client.connect()) crash #13

Closed DAOCUONG closed 4 years ago

DAOCUONG commented 4 years ago
pub async fn connect(&self) 

I tried to use task::block_on (async-std) for above connect function it crashed

davidMcneil commented 4 years ago

Unfortunately, rants only supports the tokio runtime. This is because there is currently no runtime agnostic way to spawn a future. This relates to the discussion here.

DAOCUONG commented 4 years ago

Thanks you for your info, i need to use actor model for handle nat connection there for i cant call client.connect from async function. I want to block_on client.connet(). as you mention tokio runtime can do that i have tried following code:

let f = natsclient.connect();

let mut rt = Runtime::new().unwrap();
rt.block_on(f);

I got error lock_on(f); | ^ the trait futures::future::Future is not implemented for impl core::future::future::Future

Any suggestion what api i should use to block on client.connect

DAOCUONG commented 4 years ago

FYI Using futures::executor also crashed

let f = natsclient.connect();
futures::executor::block_on(f);
davidMcneil commented 4 years ago

I added an example of using the client in a blocking context. Although, if you are using the client synchronously it might be better to use one of the other clients.

DAOCUONG commented 4 years ago

@davidMcneil thanks you so much for helping i absolutely want to do async way, i using nats client with actor model (riker) super visor will send message for all worker to connect , i would like use client.connect async way too

davidMcneil commented 4 years ago

@DAOCUONG Cool, sounds like an interesting use case. I found this issue in riker that might answer some questions in regard to spawning a future within an actor.

I am going to close this for now, but feel free to reopen or open another issue if you run into more problems. Thanks for using the library!