Created
May 12, 2021 22:05
-
-
Save alecordev/6f6523f38443f52e4ddf45f317ed16ed to your computer and use it in GitHub Desktop.
Process pool with concurrency io loops thread pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import asyncio | |
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed | |
class SyncWork: | |
def __init__(self): | |
self.active_tasks_limit = 10 | |
def __call__(self, *args, **kwargs): | |
pass | |
class AsyncWork: | |
def __init__(self): | |
pass | |
async def __call__(self, *args, **kwargs): | |
pass | |
async def iotask(): | |
print("One") | |
await asyncio.sleep(1) | |
print("Two") | |
return 1 | |
async def iorun(): | |
return await asyncio.gather(*[iotask() for _ in range(4)]) | |
def turboio(): | |
return asyncio.run(iorun()) | |
def task(): | |
time.sleep(3) | |
return "task slept 3s" | |
def tpool(): | |
c = 0 | |
with ThreadPoolExecutor() as pool: | |
futures = {pool.submit(task) for _ in range(16)} | |
for future in as_completed(futures): | |
print(future.result()) | |
c += 1 | |
print(c) | |
def ppool(): | |
with ProcessPoolExecutor() as pool: | |
# for task in tasks: | |
futures = {pool.submit(turboio) for _ in range(4)} | |
for future in as_completed(futures): | |
print(future.result()) | |
def main(): | |
ppool() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment