noirello / bonsai

Simple Python 3 module for LDAP, using libldap2 and winldap C libraries.
MIT License
117 stars 33 forks source link

Support async/await syntax #12

Closed Gr1N closed 6 years ago

Gr1N commented 7 years ago

Hi,

If we are talking about asyncio and Python 3.5+ we want to use async/await syntax instead of coroutine decorator and yield from.

Here an example how I see it:

import asyncio
import bonsai

async def do():
    client = bonsai.LDAPClient('...')
    client.set_credentials('SIMPLE', ('...', '...'))
    client.set_cert_policy('allow')

    async with client.connect(is_async=True, timeout=10.0) as conn:
        res = await conn.search('...', 2)
        print(res)
        who = await conn.whoami()
        print(who)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(do())
    loop.run_until_complete(do())

Is it possible?

noirello commented 7 years ago

Hi, Almost possible. The async with is not supported yet: I'm creating a ticket to fix this issue in the next release. So your example code can run with 0.9.0 if you do the context managing explicitly:

...
conn = await client.connect(is_async=True, timeout=10.0)
res = await conn.search('ou=nerdherd,dc=bonsai,dc=test', 2)
print(res)
who = await conn.whoami()
print(who)
conn.close()
...
trunet commented 6 years ago

is it possible to you push this PR into a new release soon?

noirello commented 6 years ago

Hi, yes, I'm working on it. I'll try to make new release in the next two weeks.

noirello commented 6 years ago

New release is out, thank you for your patience.