← index #8614Issue #4136
Related · medium · value 0.219
QUERY · ISSUE

Can't distinguish generator type from coroutine type in esp32 Micropython 1.17

openby joaduoopened 2022-05-03updated 2022-05-03
py-core

While doing a little personal webserver framework I wanted to be able to distinguish coroutines from generators

Here hypothetical use cases:

@app.plain()
def log_table(verb, cfg):
    # Here we do a generator to avoid out of memory problems
    for r in solar_log.measurements:
        yield '{}'.format(r)

@app.json(is_async=True)
async def solarcfg(verb, cfg):
    # Here we do an async method to call async functions
    if verb == POST:
        resp = await solar.set_config(cfg)
    else:
       resp = await solar.get_config()
    return resp

Right now I am solving this by adding an argument to the decorator @app.json(is_async=True), but I would like to avoid such thing and directly distinguish between coroutines and generators types. (in order to know when to iterate and when to await)

The following code:

import uasyncio

def test_generator():
    yield 1

async def test_coroutine():
    return 1

print(type(test_generator()))
print(type(test_coroutine()))
print(type(test_generator()) == type(test_coroutine()))

Outputs this:

>>> %Run -c $EDITOR_CONTENT
<class 'generator'>
<class 'generator'>
True

Is there another workaround (other than explicitly stating it in the decorator) to distinguish those types?

Thanks

CANDIDATE · ISSUE

esp8266: functions, generators don't support __name__

closedby pmp-popened 2018-09-14updated 2024-09-30
port-esp8266proposed-close

Hi while trying to pause/patch/resume coroutines indexed by their names in a live program i found a little divergence

async def func():pass
print( func.__name__)

µpython : AttributeError: 'generator' object has no attribute '__name__'

cpython 3.7 : func

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied