使用线程在触发器中进行基准测试的Python代码

时间:2018-11-27 03:15:17

标签: python multithreading concurrency benchmarking flops

使用线程在python中编写基准代码时遇到麻烦。我能够使线程正常工作,但无法使对象返回值。我想将这些值添加到列表中,以便可以计算出翻牌。

创建类以执行线程

class myThread(threading.Thread):

    def calculation(self):
        n=0
        start=time.time()
        ex_time=0
        while ex_time < 30:
            n+=1
            end=time.time()
            ex_time=end-start
        return ex_time 


    def run(self): 
        t = threading.Thread(target = self.calculation)
        t.start()

创建线程的功能

def make_threads(num):
    times=[]
    calcs=[]
    for i in range(num):
        print('start thread', i+1)
        thread1=myThread()
        t=thread1.start()
        times.append(t)
     #calcs.append(n)
    #when trying to get a return value it comes back as none as seen
    print(times)
#average out the times,add all the calculations to get the final numbers
#to calculate flops
    time.sleep(32) #stop the menu from printing until calc finish


def main():

    answer=1
    while answer != 0:
        answer=int(input("Please indicate how many threads to use: (Enter     0 to exit)"))
        print("\n\nBenchmark test with ", answer, "threads")
        make_threads(answer)

main()

1 个答案:

答案 0 :(得分:0)

执行此操作的两种方法:

1。使用静态变量(hacky,但高效快捷)

定义一些全局变量,然后在线程中进行操作。即:

import threading
import time

class myThread(threading.Thread):

    def calculation(self):
        n=0
        start=time.time()
        ex_time=0
        print("Running....")
        while ex_time < 30:
            n+=1
            end=time.time()
            ex_time=end-start

        self.myThreadValues[self.idValue] = ex_time
        print(self.myThreadValues)
        return ex_time 

    def setup(self,myThreadValues=None,idValue=None):
        self.myThreadValues = myThreadValues
        self.idValue = idValue


    def run(self): 
        self.calculation()
        #t = threading.Thread(target = self.calculation)
        #t.start()

def make_threads(num):
    threads=[]
    calcs=[]
    myThreadValues = {}

    for i in range(num):
        print('start thread', i+1)
        myThreadValues[i] = 0
        thread1=myThread()
        thread1.setup(myThreadValues,i)
        thread1.start()
        #times.append(t)
        threads.append(thread1)

    # Now we need to wait for all the threads to finish. There are a couple ways to do this, but the best is joining.

    print("joining all threads...")
    for thread in threads:
        thread.join()

    #calcs.append(n)
    #when trying to get a return value it comes back as none as seen

    print("Final thread values: " + str(myThreadValues))
    print("Done")
    #average out the times,add all the calculations to get the final numbers
    #to calculate flops
    #time.sleep(32) #stop the menu from printing until calc finish

def main():

    answer=1
    while answer != 0:
        answer=int(input("Please indicate how many threads to use: (Enter     0 to exit)"))
        print("\n\nBenchmark test with ", answer, "threads")
        make_threads(answer)

main()

2。正确的方法是使用流程

与异步工作通常使用的线程相比,进程旨在用于来回传递信息。请参阅此处的说明:https://docs.python.org/3/library/multiprocessing.html

查看此答案:How can I recover the return value of a function passed to multiprocessing.Process?

import multiprocessing
from os import getpid

def worker(procnum):
    print 'I am number %d in process %d' % (procnum, getpid())
    return getpid()

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes = 3)
    print pool.map(worker, range(5))