Giter Site home page Giter Site logo

Improve usability of `spawn` about xtra HOT 10 CLOSED

restioson avatar restioson commented on May 22, 2024
Improve usability of `spawn`

from xtra.

Comments (10)

Restioson avatar Restioson commented on May 22, 2024

I'd love to see something like spawn_default which would use a sensible default Spawner depending on the configured cargo feature

Xtra had this, but this leads to cargo features being essentially broken. If two were enabled, then what would happen? This is why I changed it to take a spawner. Mutually exclusive features are generally not good practice with cargo because of how features enabled by different dependencies are unioned together. If, for instance, a library used xtra internally with with-smol-1_1 and you used it with with-tokio-1_1, then this would cause compile errors. Furthermore, AFAIK it would not technically be a breaking change for them to enable a new feature in xtra, so this is just potential for bad times all around. spawn(&mut Tokio::Global) is indeed less ergonomic than just .spawn() but I don't know any way to do it better without breaking the features in the way that I described.

But spawning a new actor depends on the runtime, so that I have to provide an implementation myself

You should be able to just take &mut Spawner like xtra does currently and use that, I believe. Are there any issues with this approach?

from xtra.

Restioson avatar Restioson commented on May 22, 2024

I thought of one possible way to do this. There could be a SpawnGlobalTokioExt and such for each, defined like so:

trait SpawnGlobalTokioExt {
    fn spawn_global(self) -> Address<Self> {
        self.create().spawn(&mut Tokio::Global)
    }
}

Then, you would use xtra::spawner::SpawnGlobalTokioExt;

from xtra.

mneumann avatar mneumann commented on May 22, 2024

@Restioson I'd have to pass-through the &mut Spawner. So this:

let a: Address<MyActor> = MyActor::from_registry().await;

would become:

let a: Address<MyActor> = MyActor::from_registry(&mut AsyncStd).await;

It's not terribly bad, but on the other hand would tie the code towards one specific async runtime.

from xtra.

mneumann avatar mneumann commented on May 22, 2024

@Restioson Your suggestion would not really help much, as my library would still need to use xtra::spawner::SpawnGlobalTokioExt for tokio, or use ... for another runtime, wouldn't it?

I think the main issue is that multiple runtimes can co-exist via the feature flags. IMHO, this does not make much sense.

I just tried to compile sqlx with multiple async runtimes and this is what happens:

error: only one of ['runtime-actix-native-tls', 'runtime-async-std-native-tls', 
'runtime-tokio-native-tls', 'runtime-actix-rustls', 'runtime-async-std-rustls', 
'runtime-tokio-rustls'] can be enabled
# Cargo.toml
sqlx = { version = "0.4.2", features = ["runtime-tokio-rustls", 
"runtime-async-std-native-tls"] }

from xtra.

Restioson avatar Restioson commented on May 22, 2024

It would need to use it yes. That would only be one line though, and save space for all the .spawn()s across the file.

I don't really want to go the sqlx compile error route here, as it still can break with cargo and dependencies as I described

from xtra.

mneumann avatar mneumann commented on May 22, 2024

That's perfectly fine for me. I can live with that. Would there be a spawn_global available for the other runtimes as well? That'd be nice. Or you could add a spawn_global function to the Spawner trait and then to the ActorManager.

from xtra.

Restioson avatar Restioson commented on May 22, 2024

Would there be a spawn_global available for the other runtimes as well?

Yea, that's the idea. Each runtime would get a {Runtime}SpawnGlobalExt trait. Then, if you could use it at the top of the file to do this

use xtra::spawner::RuntimeNameSpawnGlobalExt;

x.spawn_global();
// ...
y.spawn_global();
// ...
z.spawn_global();

and save few characters over each doing x.spawn(&mut RuntimeName::Global)

from xtra.

mneumann avatar mneumann commented on May 22, 2024

That looks great! I'd then simply add #[cfg(....)] on each of the use statements in my library and it would work.

from xtra.

Restioson avatar Restioson commented on May 22, 2024

Hi, could you test out the spawn_global_ext branch?

Also, the recommended way of writing a library with xtra would be to take the spawner as a parameter where possible, and then pass it to spawn. Is this not possible in your use case?

from xtra.

Restioson avatar Restioson commented on May 22, 2024

Please let me know if there is any interest in the functionality contained in the spawn_global_ext branch. If so, I will merge it into mainline

from xtra.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.