【问题标题】:Python multiple stock price trackerPython多个股票价格跟踪器
【发布时间】:2020-08-31 04:23:41
【问题描述】:

我正在尝试为 python 中的一组股票创建 1 分钟的股票价格跟踪服务器。我编写了一个多线程 python 程序,它为列表中的每只股票调用我的价格跟踪器函数。这会导致我计算机上的 CPU 利用率达到约 100%,并使一切变得非常缓慢。

您能否建议我应该使用的方法或任何其他工具。

附:我也尝试过多重处理。

    def f(x):
        print("Starting process : " + str(x))
        time.sleep(5)


    if __name__ == '__main__':
        while (True):
            km = datetime.now().minute
            ks = datetime.now().second
            if km % 1 == 0 and ks == 1:
                print("=>S===> / km = " + str(km) + " | " + " ks = " + str(ks))
                for x in range(20):
                    p = Process(target=f, args=(x, ))
                    p.start()
                km = datetime.now().minute
                ks = datetime.now().second
                print("=>E===> / km = " + str(km) + " | " + " ks = " + str(ks))
                time.sleep(1)
                p.join()

这是我的代码。函数 f 是我计划在每分钟开始时进行计算,它应该为 n 个股票(代码示例中为 20 个)创建单独的进程

编辑:(1)我做了以下更改,并认为它会与时钟同步,但是,下面代码中的时钟和打印不匹配。总是有 6 秒的延迟。


from multiprocessing import Process
import os
import time

firstRun = True


def f(x):
    print("Starting process : " + str(x))
    time.sleep(5)
    print("Ending process : " + str(x))


if __name__ == '__main__':
    while (True):
        ks = datetime.now().second
        print("//////////////////////////////////////////// Delay in secs : " +
              str(ks))
        if (firstRun):
            ks = datetime.now().second
            print("First Run - Sleeping for " + str(60 - ks) + " secs.")
            firstRun = False
            time.sleep(60 - ks)
        else:
            print("Subsequent runs ...")
            ks = datetime.now().second
            #print("=>S===> / ks = " + str(ks))
            for x in range(20):
                p = Process(target=f, args=(x, ))
                p.start()
            ks = datetime.now().second
            #print("=>E===> ks = " + str(ks))
            p.join()
            print("Will sleep for " + str(60 - ks) + " secs.")
            time.sleep(60 - ks)

输出:

//////////////////////////////////////////// Delay in secs : 6
Subsequent runs ...
Starting process : 2
Starting process : 0
Starting process : 1
Starting process : 3
Starting process : 5
Starting process : 4
Starting process : 14
Starting process : 9
Starting process : 15
Starting process : 11
Starting process : 17
Starting process : 6
Starting process : 8
Starting process : 12
Starting process : 18
Starting process : 16
Starting process : 13
Starting process : 10
Starting process : 19
Starting process : 7
Ending process : 2
Ending process : 0
Ending process : 1
Ending process : 3
Ending process : 5
Ending process : 4
Ending process : 14
Ending process : 9
Ending process : 15
Ending process : 11
Ending process : 17
Ending process : 6
Ending process : 12
Ending process : 8
Ending process : 18
Ending process : 16
Ending process : 13
Ending process : 10
Ending process : 19
Ending process : 7
Will sleep for 54 secs.

【问题讨论】:

  • 添加代码和/或展示您所做的研究是惯例。
  • 您唯一的一次sleep 是每分钟一次。剩下的时间花在自旋循环中,当然你会使用 100% 的 CPU。
  • 所有整数 mod 1 都是 0,所以 km % 1 == 0 没有为你做任何事情。如果你想每分钟执行一次,你可能想sleep(60)
  • @MarkRansom,您能建议一种避免这种情况的方法吗?
  • @Josh,在这种情况下,如果我的函数 f 运行 10 秒,那么到子进程加入主进程时,经过的总时间将为 70 秒。不是吗?

标签: python python-3.x algorithmic-trading


【解决方案1】:

使用“Schedule”库解决了这个问题:

import schedule

.
.
.
schedule.every(1).minutes.at(":00").do(run_threaded, job)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多