100% CPU utilization during paper trading
zhemaituk opened this issue · 3 comments
When running readme paper trading sample - CPU utilization is ~100%.
Most of the cpu ticks spent in the following loop of alpacastore.py (_t_order_create):
while True:
try:
if self.q_ordercreate.empty():
continue
Adding simple _time.sleep(0.05)
before continue
reduces cpu consumption to about 0.02%.
I can create a pull request if such fix is correct way of addressing the problem.
Thanks for posting this fix, worked for me.
I think time.sleep
is a valid way of handling it. Alternatively could leverage the blocking timeout of Queue.get
which would achieve the same goal with less code and without the explicit is empty check, albeit with more exception handling:
try:
msg = self.q_ordercreate.get(timeout=1)
if msg is None:
continue
The granularity of Queue.get
is in seconds which is IMO not ideal, but at least we aren't busy waiting on queue.empty. Either way the code needs to be changed to stop hammering the CPU. There are a number of other places in the code where a thread does a while True
busy poll of the a queue. IMO they should all be changed. I'll try to cobble together a PR at some point.
What's the status of this? I'd be happy to help.