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

No way to make a new thread #2

Open
mikrosk opened this issue Dec 18, 2012 · 5 comments
Open

No way to make a new thread #2

mikrosk opened this issue Dec 18, 2012 · 5 comments

Comments

@mikrosk
Copy link

mikrosk commented Dec 18, 2012

I've started to investigate this issue here: signal11/hidapi#93 -- as it turned out, it was a simple mistake. So definitely, python + hidapi have some trouble together.

I see both 'while 1' and 'while 2' but then the first thread (the main one) is frozen. raw_input() doesn't work anymore. If I put everything in the same thread, it works.

from threading import Thread
import hid

def receive_handler():
    print 'while 2'

    while True:
        report = handle.read(64)
        if report:
            #print HID_COMMAND(report[0]).toString()
            #if report[0] == HID_COMMAND.USB_HIDCMD_GET_DEVICETYPE:
            #    print "Version: {0}.{1}.{2}".format(report[49], report[50], report[51])
            print '{0} bytes read, first byte: {1}'.format(len(report), hex(report[0]))
        else:
            print 'no bytes read'

handle = hid.device(0x0911, 0x251c)

handle.set_nonblocking(False)

receive_thread = Thread(target=receive_handler)
receive_thread.start()

print 'while 1'

while True:
    key = raw_input()
    print 'key ' + key + 'pressed'
    if key == 'w':
        report = bytearray(65)
        report[1] = 0x82 # command which issues a reply from my device
        handle.write(report)

    elif key == 'q':
        break

handle.close()
@qdot
Copy link
Owner

qdot commented Dec 18, 2012

Is HIDAPI even threadsafe like that? I thought the handle initialization had to live on the same thread?

@mikrosk
Copy link
Author

mikrosk commented Dec 18, 2012

According to issues/questions in the original signal11 tracker, it is. Specially when C implementation does work (see the linked issue above). How else you would poll for events?

@qdot
Copy link
Owner

qdot commented Dec 18, 2012

There's many ways to poll for events without threads. That said, like you mentioned, according to signal11/hidapi#56 hidapi is threadsafe across platforms, so yeah, should probably work in python too. Could be that the cython structure wrapping hidapi isn't threadsafe or something. I currently know very little of cython (I forked because cython-hidapi was missing some functions I needed and the main repo is defunct), but I guess now is as good a time as any to learn. :)

@mikrosk
Copy link
Author

mikrosk commented Dec 18, 2012

I think its not caused by cython. I implemented also a C api version with direct calls to hidapi and it behaves same. It seems more like a collision of python and hidapi threading system but I can't imagine how that could happen.

@mikrosk
Copy link
Author

mikrosk commented Dec 19, 2012

After a lot of headache I managed to solve it. By a workaround, of course. I replaced hid_read() with hid_read_timeout + added a little sleep in receive_handler(). I still don't understand how this can work in plain C and cannot work in C Api and/or Cython but at least I've got a solution. If you find the real cause, please post it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants