rustasync / runtime

Empowering everyone to build asynchronous software
https://docs.rs/runtime
Apache License 2.0
862 stars 28 forks source link

panicked at 'the runtime has not been set' #74

Closed iMiamas closed 4 years ago

iMiamas commented 5 years ago

when i run application to this: let stream = TcpStream::connect(server).await?;

rootthread ':' panicked at 'the runtime has not been set '

i use rust as a lib, does not have main func ,so i can not use "#[runtime:main]",or i don't kown how to use "#[runtime:main]"

iMiamas commented 5 years ago

i want to kown how can i solve this problem?

yoshuawuyts commented 5 years ago

When you say: "I use Rust as a lib" does that mean you're then calling it from C/C++? There's been conversations about writing a non-attribute version of the entry macros (likely block_on, see in part #73) which may help with this.

But currently it's not possible quite yet; apologies!

iMiamas commented 5 years ago

yes,i calling api(writted by rust) from c,and in my rust api progrem, i use runtime packge and the problem as above。thank you so much!

stbuehler commented 5 years ago

I have a similar problem with clap: as the clap::App isn't Send I had to split the main function into a sync (clap) and an async part. Given I need a nightly compiler anyway (i.e. it might break anytime) I didn't mind using the hidden runtime::raw::enter like this:

// sync part of main
runtime::raw::enter(runtime_tokio::TokioCurrentThread, async move {
    // async part of main
});