#!/usr/local/bin/python3 # To demonstrate the problem, run this with: # # /bin/sh -c "ulimit -u 30; ./example 2>&1 | tee example.log" # import asyncio import sys import uuid import traceback async def run(_id): while True: try: _uuid = uuid.uuid4().hex proc = await asyncio.create_subprocess_exec('/bin/sh','-c', f'/bin/echo {_id} > {_uuid}.out') await proc.wait() if proc.returncode == 0: break except Exception as err: exc_info = sys.exc_info() traceback.print_exception(*exc_info) await asyncio.sleep(10) return _id, proc.returncode async def main(): i = 0 tasks = [] err = 0 while i < 20: tasks.append(asyncio.create_task(run(i))) i += 1 for coro in asyncio.as_completed(tasks): _id, returncode = await coro if returncode != 0: print(f'{_id} returncode {returncode}') err += 1 print(f'{err} total errors') asyncio.run(main())