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

[feature] Using cbor2 to read streams asynchronously in an asyncio application #76

Open
AndreaCensi opened this issue Mar 15, 2020 · 2 comments

Comments

@AndreaCensi
Copy link

Hi,

I would like to use cbor2 to read CBOR streams in the context of an asyncio application.

From what I see, it is not possible now. Is that right?

From looking at the code, it looks like one should do the read() function of Decoder async, and then propagate the changes; each

value = self.read(length)

becomes

value = await self.read(length)

So I suppose it is not a quick change to be done, as most things need to become asynchronous.

Regarding the C extension, I am not sure what would be involved.

@Sekenre
Copy link
Collaborator

Sekenre commented Mar 17, 2020

Hi,

This code probably won't run but explains how I would approach the problem.

I haven't used async much, but it should work fine if you read from your stream into a buffer and then try to decode that. e.g.

async def read_cbor(stream, q):
    buf = await stream.read(1024) # or whatever maximum length your objects might be
    if (buf):
        try:
             item = cbor2.loads(buf)
             await q.put(item)
        except (CBORDecodeError, CBOREOFError):
             pass # Actually save the buffered data until you have more

And then later on, write an async function that consumes the decoded objects from the queue.

@Sekenre
Copy link
Collaborator

Sekenre commented Mar 30, 2020

I have written an example asyncio protocol using CBOR as a learning project.

You can have a look at it here: https://repl.it/repls/CoolAlertUpgrades

I will also include this example in a future documentation update.

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

No branches or pull requests

2 participants