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

[WIP] Begin implementation of asynchronous transfers, Issue #63 #303

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mrnuke
Copy link

@mrnuke mrnuke commented Apr 12, 2020

The purpose of these patches is to allow asynchronous USB transfers. Keep in mind that I am not claiming this should be the final form. I'm not an asyncio expert, and I am open for suggestions on how to improve the implementation. Note that async transfers are only imlemented for the libusb1 backend.
I am striving to achieve an API that looks like the following example:

async def do_usb_stuff():
    dev = usb.core.find(...)

    transfer1 = dev.submit_read(...)
    transfer2 = dev.submit_write(...)

    await transfer2.result()
    await transfer1.result()

Note that this is symmetrical to the sync functions, in that the transfer type does not have to be specified. It is derived from the endpoint.

await transfer.result() is similar to an asyncio.Future, although the implementation is not exactly an awaitable future. This is an implementation detail, and it could be changed if needed.

This function is not available as a library symbol, as it is inlined
in libusb. It is extremely dangerous, and we must deal with it.
This function is not available as a library symbol, as it is inlined
in libusb. It is extremely dangerous, and we must deal with it.
@mcuee
Copy link
Member

mcuee commented Jul 15, 2020

https://github.com/vpelletier/python-libusb1
Not so sure if the implementation in python-libusb1 is of some reference value here.

@mcuee
Copy link
Member

mcuee commented Aug 2, 2021

This is a WIP to address #63.

@mcuee mcuee changed the title [WIP] Begin implementation of asynchronous transfers [WIP] Begin implementation of asynchronous transfers, Issue #63 Aug 2, 2021
It seems benign, since transfers that complete earlier will have been
marked as donem and not run libusb_handle_events(). However, is this a
correct way to handle events?
TODO 2: Is libusb_handle_events() really non-blocking?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope 😕 I tried to test on my receipt printer:

from escpos.printer import Usb

p = Usb(0x2730, 0x2002, None, 0, 0x81, 0x02)
print("#print text")
p.text(f"{'ПУТИН ХУЙЛО ' * 300}")
print("#print qr")
p.qr("All the things she said", size=16, model=2)
print("#cut")
p.cut(feed=False)

Changed the function to show execution time:

        import time
        while not self._fut.done():
            st = time.time()
            _check(_lib.libusb_handle_events(self._ctx))
            print(round(time.time()-st, 5))
            await asyncio.sleep(0.0001)
#print text
4e-05
0.00304
#print qr
2e-05
1.71189
1e-05
1e-05
#cut
2e-05

@mcuee
Copy link
Member

mcuee commented Apr 7, 2024

Not sure if anyone is working on this or not. I tend to think this is one major missing feature (along with hotplug).

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

Successfully merging this pull request may close these issues.

None yet

3 participants