omnilib / aiomultiprocess

Take a modern Python codebase to the next level of performance.
https://aiomultiprocess.omnilib.dev
MIT License
1.78k stars 101 forks source link

Can't get the example Pool code to work #183

Open Deville-ee opened 1 year ago

Deville-ee commented 1 year ago

Description

I can't execute the example code in the Process section. Both the apply() method as the map(math.sqrt) method.

Documentation

I've managed to get the math.sqrt working but not like indicated in the example. I moved the math.sqrt call to an asyncio aware section because if I don't, the interpreter complains about exactly this.

TypeError: An asyncio.Future, a coroutine or an awaitable is required

Edit: To be complete : I'm getting following error when using the apply() method:

RuntimeError: await wasn't used with future

_ My adaptation for the sqrt example:

import asyncio
import math
from aiomultiprocess import Pool

async def sqrt_task(data):
    return math.sqrt(data)

async def main():
    async with Pool() as pool:
        data = [1, 4, 9, 16, 25]
        results = await pool.map(sqrt_task, data)
        print(results)

if __name__ == '__main__':
    asyncio.run(main())

Details

wbarnha commented 1 year ago

You may want to take a look at https://stackoverflow.com/questions/64429359/how-to-use-aiomultiprocess. Reapplying the example provided, you should be able to run this:

import asyncio
import math
from aiomultiprocess import Pool

async def sqrt_task(data):
    return math.sqrt(data)

async def main():
    async with Pool() as pool:
        data = [1, 4, 9, 16, 25]
        async for result in pool.map(sqrt_task, data):
           print(result)

if __name__ == '__main__':
    asyncio.run(main())