Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeprecationWarning: The loop argument is deprecated since Python 3.8, and scheduled for removal in Python 3.10 #140

Open
Teraskull opened this issue Mar 3, 2022 · 3 comments
Assignees
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@Teraskull
Copy link
Contributor

Running the example code on Python 3.9:

await asyncio.gather(bulb1.turn_on(PilotBuilder(warm_white=255)), bulb2.turn_on(PilotBuilder(warm_white=255)), loop=loop)

Shows a DeprecationWarning:

DeprecationWarning: The loop argument is deprecated since Python 3.8, and scheduled for removal in Python 3.10

Remove loop=loop for Python 3.10 compatibility.

@sbidy
Copy link
Owner

sbidy commented Mar 5, 2022

According to the documentation, the loop parameter can be removed and should be replaced by asyncio.run - https://docs.python.org/3/library/asyncio-task.html#running-an-asyncio-program

async def turn_bulbs_on(bulb1, bulb2):
  await asyncio.gather(bulb1.turn_on(PilotBuilder(warm_white=255)), bulb2.turn_on(PilotBuilder(warm_white=255)))
 
def main:
   asyncio.run(async turn_bulbs_on(bulb1, bulb2))

Code is not tested.

To be honest - the example code is a bit out of date 😄

sbidy added a commit that referenced this issue Mar 5, 2022
…n 3.8, and scheduled for removal in Python 3.10 #140
@sbidy sbidy self-assigned this Mar 5, 2022
@sbidy sbidy added enhancement New feature or request documentation Improvements or additions to documentation labels Mar 5, 2022
@wthueb
Copy link

wthueb commented Aug 17, 2022

This issue isn't fixed by 5164976. Try the following:

async def light_on():
    light = wizlight(BULB_IP)
    await light.turn_on()

asyncio.run(light_on())

I haven't looked that deeply into fixing it, as I have a very vague understanding how how WiZ light networking works, but the exception is caused by the pywizlight.wizlight.__del__ function attempting to use the event loop (calling self._async_close), however there is no guarantee that the __del__ function gets called while there's still an event loop. In the above code, light.__del__ gets called after the asyncio.run call returns, thus the event loop is closed already.

The example code produces a deprecation exception as well, since calling asyncio.get_event_loop() is deprecated when it isn't called in an event loop. The fix is not as simple as replacing the example code with loop = asyncio.new_event_loop() since the pywizlight.wizlight.__del__ function still attempts to destroy the event loop after it is already closed (I assume due to the order of which objects are deleted by the interpreter). Also, "Application developers should typically use the high-level asyncio functions, such as asyncio.run(), and should rarely need to reference the loop object or call its methods."

Removing the pywizlight.wizlight.__del__ function appears to avoid all of the issues, however as I said, I'm not sure how the networking with the lights works/if not explicitly closing the transport even matters.

@hagemt
Copy link

hagemt commented Nov 21, 2022

tl;dr: If you're doing a quick script and want to avoid:

Exception ignored in: <function wizlight.__del__ at ...>
...
RuntimeError: Event loop is closed

One can:

import pywizlight

# in main:
del pywizlight.wizlight.__del__

To avoid the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants