[英]Thread-Safe Pandas variables
我意識到在這種情況下使用多處理模塊會更好,但是我正在服務器上添加模塊(這不是管理員)需要幾天的時間,因此我寧願早點使它工作。
我的代碼不是線程安全的,如果可能的話,我試圖找出如何使其成為線程安全的代碼。 (下面的代碼)當我不使用線程時,本地設計的模塊可以工作。
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版本,其中某些核心模塊已專門刪除?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.