pymodbus-dev / pymodbus

A full modbus protocol written in python
Other
2.16k stars 889 forks source link

Don't initialize the event loop until needed. #2177

Closed jameshilliard closed 2 months ago

jameshilliard commented 2 months ago

Initializing the event loop during class init can cause issues in some cases where the event loop is not yet running.

janiversen commented 2 months ago

How can the event loop not be running in an async application?

I know of only one situation where this happens, and that is if you create the client/servers in a sync program and then start the event loop, but I have yet to understand the benefits of that.

The reason the event loop check was moved into init, was because setup is done in init, which includes calling asyncio functions, not having a loop caused some of the code to not work properly (as in hsvinhg multiple loops).

I will review the code, but look forward to understand why you find this needed.

jameshilliard commented 2 months ago

How can the event loop not be running in an async application?

Seemed to be due to initializing at module import or something along those lines. When updating an application of ours from 3.6.4 to 3.6.8 we were hitting no running event loop errors.

The reason the event loop check was moved into init, was because setup is done in init, which includes calling asyncio functions, not having a loop caused some of the code to not work properly (as in hsvinhg multiple loops).

Ah, ok, so maybe this isn't correct then. We refactored things and got it working without this change so might not be needed.

janiversen commented 2 months ago

It is not one of our imports, it happens when you create a client/server object.