处理器实验的酸洗问题

时间:2016-04-07 03:24:30

标签: python function tkinter multiprocessing pickle

当我遇到一个与酸洗有关的非常奇怪的问题时,我正在进行一个小实验,看看我是否可以提取一些反映机器使用的CPU功能的数字,尽管这似乎是由tkinter提出,因为我没有听说过“酸洗”,直到我点击它。这是错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
  File "C:\Users\Ben\Desktop\test.py", line 79, in multicore
    p1.start()
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\context.py", line 212, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\popen_spawn_win32.py", line 66, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\Ben\AppData\Local\Programs\Python\Python35-32\lib\multiprocessing\reduction.py", line 59, in dump
    ForkingPickler(file, protocol).dump(obj)
AttributeError: Can't pickle local object 'multicore.<locals>.one'

这是相关的代码: 首先,调用函数的行:

button2 = ttk.Button(frame, text = "Run Benchmark 2", command = multicore).grid(row = 2, column = 2)

第二,功能:

def multicore():
    var1 = 1
    var2 = 1
    var3 = 1
    var4 = 1
    var5 = 1
    var6 = 1
    spent1 = 0
    spent2 = 0
    spent3 = 0
    spent4 = 0
    spent5 = 0
    spent6 = 0

    start = time.time()
    cores = multiprocessing.cpu_count()

    def one():
        while var1 <= 1000000000:
            var1 = var1 * 1.0000001
        spent1 = time.time() - start

    def two():
        while var2 <= 1000000000:
            var2 = var2 * 1.0000001
        spent2 = time.time() - start

    def three():
        while var3 <= 1000000000:
            var3 = var3 * 1.0000001
        spent3 = time.time() - start

    def four():
        while var4 <= 1000000000:
            var4 = var4 * 1.0000001
        spent4 = time.time() - start

    def five():
        while var5 <= 1000000000:
            var5 = var5 * 1.0000001
        spent5 = time.time() - start

    def six():
        while var6 <= 1000000000:
            var6 = var6 * 1.0000001
        spent6 = time.time() - start

    if __name__=='__main__':
        p1 = Process(target = one)
        p2 = Process(target = two)
        p3 = Process(target = three)
        p4 = Process(target = four)
        p5 = Process(target = five)
        p6 = Process(target = six)

        if cores == 1:
            p1.start()
        elif cores == 2:
            p1.start()
            p2.start()
        elif cores == 3:
            p1.start()
            p2.start()
            p3.start()
        elif cores == 4:
            p1.start()
            p2.start()
            p3.start()
            p4.start()
        elif cores == 5:
            p1.start()
            p2.start()
            p3.start()
            p4.start()
            p5.start()
        elif cores == 6:
            p1.start()
            p2.start()
            p3.start()
            p4.start()
            p5.start()
            p6.start()

    totalspent = spent1 + spent2 + spent3 + spent4 + spent5 + spent6

    multiresult.set(round(8000 - (totalspent * 100)))

应该发生的是该函数将占用CPU内核的数量并启动许多循环乘法以查看它们完成的速度,然后将其转换为与其他计算机的分数相当的分数。另外,请原谅我过分的'tard状态,因为我不是最伟大的程序员。

编辑:通过移动defore()来解决问题:多核()

之外的语句

0 个答案:

没有答案