olijeffers0n / rustplus

Rust+ API Wrapper Written in Python for the Game: Rust
https://rplus.ollieee.xyz/
MIT License
108 stars 28 forks source link

Use a supplied event loop #27

Closed psykzz closed 1 year ago

psykzz commented 1 year ago

When running this with other software, you may want to provide your own event loop. This is important when rustplus is run in threads where by default the eventloop isn't present.

olijeffers0n commented 1 year ago

By this I assume that you are referring to the event loops that are used for running the listeners and callback functions. Could you just clarify that?

I seem to remember looking at this in the past. I basically had some issue where if a specific event loop wasn’t used it the future would never complete. I have another bug that I think may be related to the loops so will take a look at the same time 👍

psykzz commented 1 year ago

I am talking about the asyncio event loop used for coroutines.

In my example if i use discord.py which has a loop tied to the bot account, and i spin up some rustplus instances the new threads created didn't have event loops.

So ideally i like to just pass through an existing loop into the thread to have it all running together.

I assume this will have an impact (some loops wont need to be shared) but its better to start with the one, then find where we can split them up.

olijeffers0n commented 1 year ago

Sure, I have fixed this with a commit I made earlier today with version 5.5.1. Simply pass an “event_loop” kwarg to the RustSocket constructor and this will be the event loop that executes the event listeners and command executors. If this param is not present, then it will default to the loop that is running when connect is called.

Obviously, if the loop isn’t running for whatever reason they won’t execute.

psykzz commented 1 year ago

Testing this out, this did work as expected!