Pythonsスレッディングモジュールをクラス内の1つの関数に使用して成功しましたが、クラス内の複数の関数に拡張したいと考えています。たとえば、私はいくつかのデータを解析するプログラムを持っています。私はメインクラスを持っており、メインクラスには処理されるデータとは異なることを行う複数の関数があります。各関数は、特定の条件が満たされたときに呼び出されます。ここで私のプログラムに似た作られた機能を備えたいくつかの構成されたプログラムがあります。Pythonのスレッディングモジュールでクラス内で複数の関数をマルチスレッドする
class MainClass():
def __init__(self):
while True:
rawData=self.receiveData(file) #a made up function to receive data
stuffOne, stuffTwo, stuffThree, stuffFour, data=self.MainParseFunction(rawData) #returns four things and some data
if stuffOne=="a":
self.functionOne(data)
print("Output of Function One")
elif stuffTwo=="b":
self.functionTwo(data)
print("Output of Function Two")
elif stuffThree=="c":
self.functionThree(data)
print("Output of Function Three")
elif stuffFour=="d":
self.functionFour(data)
print("Output of Function Four")
def MainParseFunction(self, data):
'''Do some stuff to the data being passed to my function and return a bunch of variables to be use in the other functions '''
def functionOne(self, data):
'''Do some stuff to the data being passed to my function and return a bunch of variables to be printed '''
def functionTwo(self, data):
'''Do some stuff to the data being passed to my function and return a bunch of variables to be printed '''
def functionThree(self, data):
'''Do some stuff to the data being passed to my function and return a bunch of variables to be printed '''
def functionFour(self, data):
'''Do some stuff to the data being passed to my function and return a bunch of variables to be printed '''
if __name__ == ('__main__'):
MainClass()
私の実際のプログラムはより複雑で実際に処理をスピードアップするためにスレッドを使用したい多くのデータを処理しますが、私はそれが呼び出されるときに1つの関数を呼び出すときにスレッドをしたいと思います。私が見たほとんどの例は、複数の関数ではない単一の関数を対象にしています。私はこれが可能であると仮定します。私はこれについてどうやって行くのか分かりません。
def threader():
while True:
job=self.q.get()
self.MainParseFunction(job)
self.q.task_done()
for _ in range(10):
t=threading.Thread(target=self.functionOne)
t.daemon=True
t.start()
for job in range(1,500):
self.q.put(job)
self.q.join()
それぞれがターゲットとして機能するスレッドを1つ起動するだけでは不十分ですか?つまり、... t1 = threading.Thread(target = self.functionOne)のようなものです。 t2 = threading.Thread(target = self.functionTwo); t3 = threading.Thread(target = self.functionThree)...などのように? – BorrajaX