0
我知道在这种情况下使用多处理模块会更好,但是我正在一台服务器上工作,需要几天的时间来添加模块(我不是管理员),而且我会宁愿早日获得这项工作。线程安全的熊猫变量
我有代码是不是线程安全的,如果可能的话,我想弄清楚如何使它线程安全。 (下面的代码)当我不使用线程时,我本地设计的模块工作。
import sys, threading
from school.school_func import run_school_report
from class.class_func import run_class_report
from common.getdata import get_data #locally designed module
import pandas as pd
class myThread (threading.Thread):
def __init__(self, task, year, month, c1, c2, data):
threading.Thread.__init__(self)
self.task = task
self.year = year
self.month = month
self.class1= c1
self.class2= c2
self.data = data
def run(self):
if self.task == "school_report":
run_school_report(self.year, self.month, self.class1, self.class2, self.data)
print("\nSchool report finished! \n")
else:
run_class_report(self.year, self.month, self.class1, self.class2)
print("\nClass reports finished!\n")
#main script
YEAR = 2017
MONTH = 3
C1, C2, DATA = get_data('mysql', reload=False, blacklist=True)
t1 = myThread("school_report", YEAR, MONTH, C1, C2, DATA)
t2 = myThread("class_reports", YEAR, MONTH, C1, C2, DATA)
t1.start()
t2.start()
t1.join()
t2.join()
print("NOTICE:\nAll tasks successfully completed!\n\n")
变量C1,C2,DATA和分配它们值的类变量是pandas.DataFrame
对象。
我的问题是这样的:
1)是否有可能使此代码线程安全的?
2)我应该咬紧牙关,使用多处理?
'multiprocessing'是一个核心python模块,如果你有'threading',你也有'multiprocessing',除非你正在运行一些特殊的Python版本,其中一些核心模块已经被特别的删除了。 –
呵呵,我对Python很新,显然,但看起来你是正确的。也许我拼错了它。多么尴尬。 – ajsmart
不用担心,我会将评论添加为答案,以便您可以接受并回答问题。 –