Re: transform a "normal" decorator in one for async functions

2018-09-19 Thread vito . detullio
Il giorno martedì 18 settembre 2018 17:36:16 UTC+2, vito.d...@gmail.com ha 
scritto:

> > > is there a way to "convert" a "normal" decorator in one that can handle 
> > > async functions?
> > In general? No.
> Ouch.
> 
> I'm new to the "async" world, but so this mean that all of the (appliable) 
> decorators present in the various libraries have to be "paired" with async 
> versions?

I was also thinking: there are some decorator functions (and decorator 
functions factories) also in the stdlib (functools.wrap, property, classmethod 
... you name it)
so this means that also we need to check each of them if is applyable to async 
functions?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: transform a "normal" decorator in one for async functions

2018-09-18 Thread vito . detullio
Il giorno martedì 18 settembre 2018 17:15:22 UTC+2, Thomas Jollans ha scritto:

> > but I cannot rewrite this library.
> 
> Perhaps not, but surely you can write your own decorator that does
> whatever this library's decorator does for async functions? Presumably
> you have the code...

well, maybe? While I could have access to the sources, I don't want to 
"maintain" (part of) this library.
Worst even if I need to maintain it in my project, and not upstream.

> > is there a way to "convert" a "normal" decorator in one that can handle 
> > async functions?
> In general? No.

Ouch.

I'm new to the "async" world, but so this mean that all of the (appliable) 
decorators present in the various libraries have to be "paired" with async 
versions?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: transform a "normal" decorator in one for async functions

2018-09-18 Thread Thomas Jollans
On 2018-09-18 16:20, vito.detul...@gmail.com wrote:
> but I cannot rewrite this library.

Perhaps not, but surely you can write your own decorator that does
whatever this library's decorator does for async functions? Presumably
you have the code...

> is there a way to "convert" a "normal" decorator in one that can handle async 
> functions?

In general? No.


-- 
https://mail.python.org/mailman/listinfo/python-list


transform a "normal" decorator in one for async functions

2018-09-18 Thread vito . detullio
Hi.

Let's say I have this library that expose a decorator; it's meant to be used 
with normal functions.

but I have to "apply" the decorator to an async function.
>From what I understand I cannot "really" wait for this decorated async 
>function, as it's not really "async", and, from a simple test I made, I see 
>that the order of execution is not really "straight" (I have simplifield the 
>example to the bone)

import asyncio

def deco(fun): # the "classic" decorator
def wrap(*args, **kwargs):
print('wrap begin')
try:
return fun(*args, **kwargs)
finally:
print('wrap end')
return wrap

@deco
async def decorated_async():
print('a')
await asyncio.sleep(1)
print('b')


loop = asyncio.get_event_loop()
loop.run_until_complete(decorated_async())
loop.close()

the output of this script is

wrap begin
wrap end
a
b

while an "async-aware" decorator (with an "async" wrap that "await"s the fun 
call) would print the "wrap end" line at the end.

I can easily rewrite my dumb decorator to handle async functions (I can just 
make an "async def wrap" and "return await fun"), but I cannot rewrite this 
library.

is there a way to "convert" a "normal" decorator in one that can handle async 
functions?

Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list