Python bots and the pause nobody planned
Posted: Sat Oct 31, 2026 2:29 pm
Python bots and the pause nobody planned
Python is popular for trading bots. It's quick to write and has great data libraries. Its runtime also has behaviors that matter for timing.
Garbage collection occasionally pauses the program to clean memory. Usually the pause is a millisecond. With large data structures, it can reach tens of milliseconds, at random moments.
The global interpreter lock means only one thread runs Python code at a time, so a heavy calculation in one thread can delay another that's handling price updates.
Slow functions in the hot path, such as parsing a large JSON message or recalculating an indicator over a long history on every tick, add up under load.
Mitigations include preallocating memory, avoiding creating many temporary objects in the main loop, using efficient libraries for numerical work, and separating the price-handling loop from heavy analysis.
Measuring is essential. Log the time between receiving a tick and sending an order, in microseconds, and look at the distribution. The median might be fine, while the 99th percentile hides the pauses.
For strategies where tens of milliseconds matter, a compiled language may be the better tool.
At the 99th percentile, how long does the bot's decision loop take?
Python is popular for trading bots. It's quick to write and has great data libraries. Its runtime also has behaviors that matter for timing.
Garbage collection occasionally pauses the program to clean memory. Usually the pause is a millisecond. With large data structures, it can reach tens of milliseconds, at random moments.
The global interpreter lock means only one thread runs Python code at a time, so a heavy calculation in one thread can delay another that's handling price updates.
Slow functions in the hot path, such as parsing a large JSON message or recalculating an indicator over a long history on every tick, add up under load.
Mitigations include preallocating memory, avoiding creating many temporary objects in the main loop, using efficient libraries for numerical work, and separating the price-handling loop from heavy analysis.
Measuring is essential. Log the time between receiving a tick and sending an order, in microseconds, and look at the distribution. The median might be fine, while the 99th percentile hides the pauses.
For strategies where tens of milliseconds matter, a compiled language may be the better tool.
At the 99th percentile, how long does the bot's decision loop take?