alpacahq / alpaca-backtrader-api

Alpaca Trading API integrated with backtrader
https://pypi.org/project/alpaca-backtrader-api/
Apache License 2.0
618 stars 144 forks source link

100% CPU utilization during paper trading #129

Open zhemaituk opened 3 years ago

zhemaituk commented 3 years ago

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.

k- commented 3 years ago

Thanks for posting this fix, worked for me.

jho commented 3 years ago

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.

mturiansky commented 3 years ago

What's the status of this? I'd be happy to help.