FlorianREGAZ / Python-Tls-Client

Advanced HTTP Library
MIT License
678 stars 135 forks source link

Async #36

Open CyberParadise opened 1 year ago

CyberParadise commented 1 year ago

Hi, is it possible to use it asynchronously?

MsLolita commented 1 year ago

I've also been wondering for a long time about being able to use this asynchronously, because you won't do much synchronously.

I use decorators :

import time
import os
import asyncio
from functools import wraps, partial

def async_wrap(func):
    @wraps(func)
    async def run(*args, loop=None, executor=None, **kwargs):
        if loop is None:
            loop = asyncio.get_event_loop()
        pfunc = partial(func, *args, **kwargs)
        return await loop.run_in_executor(executor, pfunc)
    return run 

@async_wrap
def my_async_sleep(duration):
    time.sleep(duration)
CyberParadise commented 1 year ago

68 check this