# RuntimeError: Cannot create subprocess in event loop that is not running

- **ID:** `python/asyncio-cannot-create-subprocess`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to create an asyncio subprocess (e.g., via asyncio.create_subprocess_exec) before the event loop is started or after it is stopped.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |
| 3.10 | active | — | — |

## Workarounds

1. **Use asyncio.run() to automatically manage the event loop lifecycle** (95% success)
   ```
   async def main(): proc = await asyncio.create_subprocess_exec('cmd'); asyncio.run(main())
   ```
2. **Ensure the loop is running before creating subprocess** (90% success)
   ```
   loop = asyncio.new_event_loop(); asyncio.set_event_loop(loop); loop.run_until_complete(create_subprocess())
   ```

## Dead Ends

- **Using asyncio.get_event_loop() to get a loop and then calling run_until_complete** — get_event_loop() may return a closed loop; subprocess creation requires a running loop. (60% fail)
- **Setting the event loop policy to WindowsProactorEventLoopPolicy** — This only applies to Windows; the error is about loop state, not platform. (40% fail)
