Skip to main content

A simple library for unblocking asynchronous code

Project description

as-async

Small python library to unblock sync functions and generators

Installation

    pip install as-async

Examples

basic usage

   import time
   import asyncio
   from concurrent.futures import ThreadPoolExecutor

   from as_async import as_async

   @as_async()
   def long_running_function():
       time.sleep(5)
       return "Finished"

   async def main():
       result = await long_running_function()
       print(result)

   asyncio.run(main())

specify loop and or executor

    import time
    import asyncio
    from concurrent.futures import ThreadPoolExecutor

    from as_async import as_async_generator

    @as_async_generator(loop=asyncio.get_event_loop(), executor=ThreadPoolExecutor(max_workers=5))
    def long_running_generator():
        for i in range(5):
            time.sleep(1)  # Simulate a long-running operation
            yield i

    async def main():
        async for value in long_running_generator():
            print(value)

    asyncio.run(main())

equivalents to the above without the decorator also exists

   import time
   import asyncio
   from concurrent.futures import ThreadPoolExecutor

   def long_running_function():
       time.sleep(5)
       return "Finished"


   async def main():
       async_func = to_async(long_running_function)
       result = await async_func()
       print(result)

   asyncio.run(main())

License

This project is licensed under the MIT License - see the LICENSE file for details

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

as-async-1.0.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

as_async-1.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file as-async-1.0.1.tar.gz.

File metadata

  • Download URL: as-async-1.0.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.0

File hashes

Hashes for as-async-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7c881d902128fd362f38c41c3a1e373f967c978f3cd87fcb0757780a001b96aa
MD5 120480778e10f9c4b648e594eaf90e55
BLAKE2b-256 47f370a8e738fd6594f38bf11a93b4b9e9585575d5553d86529cfacb259d171b

See more details on using hashes here.

File details

Details for the file as_async-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: as_async-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.0

File hashes

Hashes for as_async-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93d16a4eb49227979bbcecd4ec997d6193a9a2e60c40a0a00b73574f8f9ef2ed
MD5 285e8c4aed50ce2a6d21ff5862e53649
BLAKE2b-256 9c78812b255682bb220db0d4b83f7a39b92a2ff6f1904284d9782f364c2ed74e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page