提问人:Alex Bergandine 提问时间:11/15/2023 最后编辑:Alex Bergandine 更新时间:11/15/2023 访问量:51
有没有办法在python中的两个不同内核上同时运行多个进程?
Is there a way to run multiple processes on two different cores in python at the same time?
问:
我正在使用 tkinter 和 customtkinter 构建一个应用程序。我希望它每秒更新一次屏幕,但我希望它每 3 秒获取股票列表中某些股票的价格 .txt 而不是我希望它将该数据报告给 datarows.txt 中的表并根据股票的当前价格更新该表。由于屏幕每秒更新一次,我希望它每秒检查一次表格,看看它说的是股票的当前价格,并将其设置为屏幕上。我让它完全按照应有的方式工作,但是当它去雅虎财经检查股票价格时,每 3 秒就会滞后程序,使其在检查数据时无法使用大约一两秒钟。以下是所有代码:
from tkinter import END
import customtkinter
from PIL import Image
import time
from yahoo_fin import stock_info as si
import datetime
import subprocess
from multiprocessing import Process
customtkinter.set_appearance_mode("Dark") # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue"
class App(customtkinter.CTk):
def __init__(self):
# Code for creating window
#
#
def Refresher():
lst = [("STOCK:", "PRICE:", "BUY/SELL:", "UP CONFIDENCE:", "DOWN CONFIDENCE:", "ACCURACY:"),
("", "", "", "", "", "")]
with open("datarows.txt", "r") as data:
data = data.readlines()
for line in data:
lists = []
if len(line) > 10:
line = line.split(", ")
for string in line:
string = string.split("=")[1]
lists.append(string)
line = tuple(lists)
lst.append(line)
total_row = len(lst)
total_column = len(lst[0])
for i in range(total_row):
for j in range(total_columns):
self.e = customtkinter.CTkEntry(self.tabview.tab("Trading"))
self.e.grid(row=i, column=j, ipadx=50)
self.e.insert(END, lst[i][j])
self.after(1000, Refresher)
Refresher()
def freshfit(self):
global lst
global total_rows
global total_columns
lst = [("STOCK:", "PRICE:", "BUY/SELL:", "UP CONFIDENCE:", "DOWN CONFIDENCE:", "ACCURACY:"),
("", "", "", "", "", "")]
with open("datarows.txt", "r") as dat:
dat = dat.readlines()
for line in dat:
thelist = []
if len(line) > 10:
line = line.split(", ")
for strings in line:
string = strings.split("=")[1]
thelist.append(strings)
line = tuple(thelist)
lst.append(line)
# find total number of rows and columns in list
total_rows = len(lst)
total_columns = len(lst[0])
joe = []
with open("stocks.txt", "r") as yeeter:
yeet = yeeter.readlines()
for line in yeet:
line = line.strip("\n")
stock = line
with open("datarows.txt", "r") as dats:
daters = dats.readlines()
jake = []
for jim in daters:
jake.append(jim)
if stock in jim:
for bob in jake:
if stock in bob:
live_prices = si.get_live_price(stock)
bob = bob.split(", ")
bob[1] = f"price={round(live_prices, 2)}"
bob = ", ".join(bob)
joe.append(str(bob) + "\n")
with open("datarows.txt", "w") as writs:
writs.write("")
with open("datarows.txt", "a") as apens:
for parts in joe:
parts.strip("[[\n]]")
apens.write(str(parts))
lst.append(parts)
self.after(3000, self.freshfit)
lst = [("STOCK:", "PRICE:", "BUY/SELL:", "UP CONFIDENCE:", "DOWN CONFIDENCE:", "ACCURACY:"),
("", "", "", "", "", "")]
with open("datarows.txt", "r") as datas:
datas = datas.readlines()
for line in datas:
list = []
if len(line) > 10:
line = line.split(", ")
for thestring in line:
thestring= thestring.split("=")[1]
list.append(thestring)
line = tuple(list)
lst.append(line)
# find total number of rows and columns in list
total_rows = len(lst)
total_columns = len(lst[0])
if __name__ == "__main__":
app = App()
p1 = Process(target=app.freshfit())
p1.start()
app.mainloop()
因此,如您所见,我尝试使用多处理来独立更新 freshfit 函数,但它仍然滞后于程序。freshfit 函数是抓取股票数据并将其插入到 datarows.txt 中的表中的函数,嵌套函数是每秒检查和更新屏幕的函数。我也意识到我的代码有些草率。我相信问题在于,即使使用多处理,它也会在同一内核上运行程序。我有一个 8 核 CPU,我相信由于它在同一内核上运行,因此无论如何它都会滞后于整个程序。我尝试了解多线程和multiprocessing.pool,但没有成功,这意味着我要么找不到实现它们的正确方法,要么它们无法解决此问题。有什么想法吗?
编辑:stocks.txt文件只是一个股票列表,所以每行都是不同的股票代码,如aapl,spy,pep等,该行上没有其他任何东西,目前stocks.txt文件有7只股票。datarows.txt 文件现在有 3 行,仅用于测试目的,但我最终会将所有股票添加到其中。这是其中一行的样子: 股票=AAPL,价格=186.96,买入/卖出=中性,上行置信度=85,下置度=15,准确性=75 股票.txt文件不会更改,保持绝对值,但是DataRows.txt文件保持不变,除了价格会根据该股票的实际当前价格实时更新。该程序这样做,但是每次它都会滞后 tkinter 窗口。
答: 暂无答案
评论
sss
ssss