question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexError While using the "Chain" Method on a function with @asyn.cancellable

See original GitHub issue

Now I have a real issue: If you use the normal await style from the chain example e.g: result = await compute(x, y) on a function with an @asyn.cancellable decorator, it will throw an: IndexError: tuple index out of range Error. (File “asyn.py”, line 327, in new_gen)

https://github.com/peterhinch/micropython-async/blob/8239a3d8b9f19430a13606c1492d5bf9a18300eb/chain.py#L15

https://github.com/peterhinch/micropython-async/blob/8239a3d8b9f19430a13606c1492d5bf9a18300eb/asyn.py#L327

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
peterhinchcommented, Sep 7, 2019

You are using the module incorrectly. There is a required syntax for calling an awaitable function. In the no-args case it is

try:
    import uasyncio as asyncio
except ImportError:
    import asyncio
import asyn

@asyn.cancellable
async def compute():
    # No args are given to the compute function.
    print("compute function")
    await asyncio.sleep(1.0)
    return 1

async def print_sum(x, y):
    print("compute function")
    result = await asyn.Cancellable(compute)
    print(result)

loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()

Or when passing args it is:

try:
    import uasyncio as asyncio
except ImportError:
    import asyncio
import asyn

@asyn.cancellable
async def compute(x, y):
    # No args are given to the compute function.
    print("compute function")
    await asyncio.sleep(1.0)
    return x + y

async def print_sum(x, y):
    print("compute function")
    result = await asyn.Cancellable(compute, x, y)
    print(result)

loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()

[EDIT] This isn’t the first time this has caused confusion. I have updated PRIMITIVES.md to include a warning about this.

Incidentally my fast_io fork fixes this bug in uasyncio V2. Tasks may be cancelled in the normal way without the need for special syntax. Paul Sokolovsky’s version fixes this bug in a more efficient way but it requires his pycopy firmware; mine uses official firmware.

0reactions
peterhinchcommented, Sep 9, 2019

I found out about pycopy

This is your choice. I only support official MicroPython firmware for a few reasons, not least because binaries are available to those who are reluctant or unable to build from source.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When does the async / await chaining stop? - Stack Overflow
Longer answer: Whenever GetDataAsync is awaiting (for instance wait until data is written to a file, or fetched from a database), your procedure ......
Read more >
Trio's core functionality — Trio 0.1.0 documentation
Here's the rule: if it's in the trio namespace, and you use await to call it, then it's cancellable (see Yield points above)....
Read more >
Promise chaining is dead. Long live async/await
In this article, we will look at how async/await really makes developers' lives easier and why you should stop using promise chaining.
Read more >
https://support.microsoft.com/js/SearchBox.Main.mi...
{ indexOf, includes }` methods implementation\nvar createMethod = function ... var index = 0;\n // variable length - can't use forEach\n while (chain.length...
Read more >
firefox security update - Oracle Linux Yum Server
Details on how to use ULN or http://public-yum.oracle.com to apply this ... Resolves: bz#1141189 (bug in scsi_block_new_request() function introduced by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found