Code Monkey home page Code Monkey logo

Comments (15)

wackazong avatar wackazong commented on July 17, 2024

If I comment out the line

self.stop()

then the generator returns all devices but never returns.

from xled.

scrool avatar scrool commented on July 17, 2024

This indeed looks like a bug and a fix that you have proposed might be the correct way to go. I need to find out if we wouldn't end up with loop that never ends though.s

On the other hand the snippet you have provided indeed never ends - there is nothing that would raise StopIteration. You'll have to handle that on your own for now - e.g. end the loop after some timeout or look at discovered devices and end when you find all of those you were looking for.

from xled.

scrool avatar scrool commented on July 17, 2024

Please try latest changes in develop branch.

On the top of that if you would like to get all devices on the network you can now use timeout like this:

    devices = []
    discovered_devices = xled.discover.xdiscover(timeout=3)
    while True:
        try:
            device = next(discovered_devices)
            devices.append(device)
        except StopIteration:
            break
    return devices

And in devices you'll have all devices that responded until 3 seconds.

from xled.

wackazong avatar wackazong commented on July 17, 2024

Thanks for the quick reaction! In the example you give a DiscoverTimeout Exception is raised when the timeout is reached. This is not taken into account in the example and the execution is halted. Why are you raising the Exception instead of just returning when the timeout is reached?

from xled.

wackazong avatar wackazong commented on July 17, 2024

Like this it works nicely. Thanks for that!

    devices = []
    discovered_devices = xled.discover.xdiscover(timeout=3)
    while True:
        try:
            device = next(discovered_devices)
            devices.append(device)
        except StopIteration:
            break
        except DiscoverTimeout:
            break
    return devices

from xled.

scrool avatar scrool commented on July 17, 2024

Why are you raising the Exception instead of just returning when the timeout is reached?

I'd like caller to get a reason why xdiscover returned. It could also fail with error.

from xled.

wackazong avatar wackazong commented on July 17, 2024

If there is no device at all the call next(discovered_devices still hangs.

from xled.

scrool avatar scrool commented on July 17, 2024

If there is no device at all the call next(discovered_devices still hangs.

With a timeout? I cannot reproduce that. If I don't have my Twinkly turned on and I run the same code I get DiscoverTimeout exception.

from xled.

wackazong avatar wackazong commented on July 17, 2024

Yes, with timeout set. Ok, I will try and put together a test case.

from xled.

wackazong avatar wackazong commented on July 17, 2024

I can reproduce it with the script above.

Stack trace when I interrupt is:

^CTraceback (most recent call last):
  File "twinkly/test_timeout.py", line 8, in <module>
    device = next(discovered_devices)
  File "/home/pi/wakeupcall/.venv/src/xled/xled/discover.py", line 74, in xdiscover
    response = interface.recv()
  File "/home/pi/wakeupcall/.venv/src/xled/xled/discover.py", line 199, in recv
    return self.pipe.recv_multipart()
  File "/home/pi/wakeupcall/.venv/lib/python3.7/site-packages/zmq/sugar/socket.py", line 475, in recv_multipart
    parts = [self.recv(flags, copy=copy, track=track)]
  File "zmq/backend/cython/socket.pyx", line 791, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 827, in zmq.backend.cython.socket.Socket.recv
  File "zmq/backend/cython/socket.pyx", line 186, in zmq.backend.cython.socket._recv_copy
  File "zmq/backend/cython/checkrc.pxd", line 12, in zmq.backend.cython.checkrc._check_rc
KeyboardInterrupt

It's always the same stack trace.

from xled.

scrool avatar scrool commented on July 17, 2024

I'm wondering if you can test this on the top of latest changes?

from xled.

wackazong avatar wackazong commented on July 17, 2024

Hm, when I run the test case with 0.6.1 installed and no devices on the network the script still does not return.

Stack trace when I interrupt:

    ^CTraceback (most recent call last):
      File "test_xled.py", line 9, in <module>
        device = next(discovered_devices)
      File "/home/pi/wakeupcall/.venv/lib/python3.6/site-packages/xled/discover.py", line 74, in xdiscover
        response = interface.recv()
      File "/home/pi/wakeupcall/.venv/lib/python3.6/site-packages/xled/discover.py", line 199, in recv
        return self.pipe.recv_multipart()
      File "/home/pi/wakeupcall/.venv/lib/python3.6/site-packages/zmq/sugar/socket.py", line 583, in recv_multipart
        parts = [self.recv(flags, copy=copy, track=track)]
      File "zmq/backend/cython/socket.pyx", line 781, in zmq.backend.cython.socket.Socket.recv
      File "zmq/backend/cython/socket.pyx", line 817, in zmq.backend.cython.socket.Socket.recv
      File "zmq/backend/cython/socket.pyx", line 186, in zmq.backend.cython.socket._recv_copy
      File "zmq/backend/cython/checkrc.pxd", line 13, in zmq.backend.cython.checkrc._check_rc
    KeyboardInterrupt

Python 3.6.3, using Poetry Package Manager.
Skript used:

    import xled
    from xled.exceptions import DiscoverTimeout
    devices = []
    discovered_devices = xled.discover.xdiscover(timeout=3)
    while True:
        print('trying')
        try:
            device = next(discovered_devices)
            print('got device')
            devices.append(device)
        except StopIteration:
            break
        except DiscoverTimeout:
            break
    print(devices)

from xled.

wackazong avatar wackazong commented on July 17, 2024

When I have devices on the network the script returns and works as expected.

from xled.

scrool avatar scrool commented on July 17, 2024

Hm, when I run the test case with 0.6.1 installed and no devices on the network the script still does not return.

From what I can tell Bad File Descriptor is no longer the case here. I'm not able to reproduce infinite loop though.

I'm using separate virtual environment on the top of latest commit in develop:

$ git describe
v0.6.1-42-g0e22aaa
$ python3.6 -m venv ~/.virtualenvs/xled@py36      
$ . ~/.virtualenvs/xled@py36/bin/activate
$ python --version
Python 3.6.12
$ python setup.py develop
...
error: The 'requests' distribution was not found and is required by xled

I think this is chicken-egg problem with pip so for now I'm dealing with this by just second run:

$ python setup.py develop
...
Finished processing dependencies for xled==0.6.1
$ pip freeze
certifi==2020.12.5
cffi==1.14.4
chardet==4.0.0
click==8.0.0a1
click-log==0.3.2
cryptography==3.3.1
idna==2.10
importlib-resources==5.1.0
netaddr==0.8.0
pycparser==2.20
pyzmq==22.0.2
requests==2.25.1
requests-toolbelt==0.9.1
six==1.15.0
tornado==6.1
urllib3==1.26.3
-e [email protected]:scrool/xled.git@0e22aaa4ae90e678b045d934a816c2cfff53d11d#egg=xled
zipp==3.4.0
$ cat test_28.py
import xled
from xled.exceptions import DiscoverTimeout
devices = []
discovered_devices = xled.discover.xdiscover(timeout=3)
while True:
    print('trying')
    try:
        device = next(discovered_devices)
        print('got device')
        devices.append(device)
    except StopIteration:
        break
    except DiscoverTimeout:
        break
print(devices)

a) with no devices:

$ timeout -v 10 /usr/bin/time -p python test_28.py
trying
[]
real 4.58
user 0.48
sys 0.06

it ends later than 3 seconds because of python startup overhead but in less than 10 seconds after timeout would kill the script.

b) with one device:

$ timeout -v 10 /usr/bin/time -p python test_28.py
trying
got device
trying
[DiscoveredDevice(hw_address='xx:xx:xx:xx:xx:xx', id='Twinkly_XXXXXX', ip_address='192.168.1.X')]
real 3.66
user 0.52
sys 0.07

c) with two devices:

$ timeout -v 10 /usr/bin/time -p python test_28.py
trying
got device
trying
got device
trying
[DiscoveredDevice(hw_address='xx:xx:xx:xx:xx:xx', id='Twinkly_XXXXXX', ip_address='192.168.1.X'), DiscoveredDevice(hw_address='yy:yy:yy:yy:yy:yy', id='Twinkly_YYYYYY', ip_address='192.168.1.Y')]
real 3.61
user 0.53
sys 0.07

Please (re)install dependencies if possible and list your packages and their versions with pip freeze.

from xled.

scrool avatar scrool commented on July 17, 2024

I have multiple devices now and I haven't been able to reproduce this issue nor seen it occurring randomly. I'm going to close this one out.

from xled.

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.