Can't distinguish generator type from coroutine type in esp32 Micropython 1.17
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
esp8266: functions, generators don't support __name__
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