noxdafox / pebble

Multi threading and processing eye-candy.
GNU Lesser General Public License v3.0
525 stars 49 forks source link

Asyncio pool support #90

Closed Bewinxed closed 2 years ago

Bewinxed commented 2 years ago

I have the following code:

async def syncTrackers():
        # trackerCollections = await magiceden.magiceden.allCollections()
        with ProcessPoolExecutor(max_workers=4) as executor:
            trackerCollections = await botutilities.collections.getTrackerCollection(con, None)
            syncFutures = [executor.submit(await nftAnalytics.dataSync.syncCollectionStats(collection['symbol'])) for collection in trackerCollections]
            results = concurrent.futures.wait(syncFutures)

I'm trying to run it using pebble, syncCollectionStats is an async function so I need to make it awaitable somehow, I tried wrapping future, running in executor, but it keeps telling me something about not being able to pickle a coroutine, I've tried a lot but can't seem to get it to work.

The goal is to run the function on each item in trackerCollections where each separate instance of the function takes no longer than 60 seconds, and run the complete queue through a separate process pool.

also what's the difference between map and schedule?

Thank you!

noxdafox commented 2 years ago

You cannot pass to a pool a coroutine as it cannot ship it to the child process/thread. You can see how to do it with concurrent.futures and asyncio in the following documentation: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor

Unfortunately, Pebble does not currently support asyncio. It's in the roadmap and I'll try to work on it in the upcoming weeks.

noxdafox commented 2 years ago

Issue resolved with release 5.0.0.