For 1 million shell script tasks, here are the most effective Python approaches, ranked by performance and scalability:
1. Asyncio with subprocess (Recommended)
import asyncio
import subprocess
async def run_script(script_path):
process = await asyncio.create_subprocess_exec(
script_path,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
return process.returncode, stdout, stderr
async def main():
semaphore = asyncio.Semaphore(100) # Limit concurrent tasks
async def bounded_task(script):
async with semaphore:
return await run_script(script)
tasks = [bounded_task(script) for script in script_list]
results = await asyncio.gather(*tasks, return_exceptions=True)
asyncio.run(main())
Pros: Extremely efficient for I/O-bound operations, low memory overhead, handles 1M+ tasks well
Cons: Requires async-aware code
2. ThreadPoolExecutor with batching
from concurrent.futures import ThreadPoolExecutor, as_completed
import subprocess
import itertools
def run_script(script_path):
result = subprocess.run([script_path], capture_output=True, text=True)
return result.returncode, result.stdout, result.stderr
def process_batch(scripts, batch_size=1000):
with ThreadPoolExecutor(max_workers=50) as executor:
futures = [executor.submit(run_script, script) for script in scripts[:batch_size]]
return [future.result() for future in as_completed(futures)]
# Process in batches to manage memory
for batch in itertools.islice(script_iterator, 0, None, 1000):
results = process_batch(batch)
Pros: Simple to implement, good for I/O-bound tasks
Cons: Higher memory usage, thread overhead
3. Celery with Redis/RabbitMQ (Best for distributed)
from celery import Celery
import subprocess
app = Celery('tasks', broker='redis://localhost:6379')
@app.task
def run_script_task(script_path):
result = subprocess.run([script_path], capture_output=True, text=True)
return result.returncode, result.stdout, result.stderr
# Submit tasks
job_results = []
for script in script_list:
job_results.append(run_script_task.delay(script))
Pros: Scales across multiple machines, fault-tolerant, monitoring
Cons: Requires additional infrastructure
4. ProcessPoolExecutor (for CPU-intensive scripts)
from concurrent.futures import ProcessPoolExecutor
import subprocess
def run_script(script_path):
result = subprocess.run([script_path], capture_output=True, text=True)
return result.returncode, result.stdout, result.stderr
with ProcessPoolExecutor(max_workers=os.cpu_count()) as executor:
futures = [executor.submit(run_script, script) for script in script_list]
results = [future.result() for future in futures]
Recommendations:
For single machine: Use asyncio - it's the most efficient for shell script execution at this scale.
For multiple machines: Use Celery with a message broker for distributed processing.
Key optimizations:
- Limit concurrent tasks (50-200 depending on system)
- Process in batches to manage memory
- Implement proper error handling and retries
- Monitor system resources (file descriptors, memory)
- Consider using
subprocess.Popen with polling for more control
The asyncio approach typically handles 1M tasks most efficiently on a single machine due to its low overhead and excellent I/O concurrency.