简体   繁体   English

Python 3.8 中的多处理

[英]Multiprocessing in Python 3.8

I'm trying to use multiprocessing in python for the first time.我第一次尝试在 python 中使用多处理。 I wrote a basic prime searching program and I want to run simultaneously it on each core.我写了一个基本的素数搜索程序,我想在每个核心上同时运行它。 The problem is: when the program does the multiprocessing, it not only does the 'primesearch' function but also the beginning of the code.问题是:当程序进行多处理时,它不仅会执行“primesearch”function,而且还会执行代码的开头。 My expected output would be a list of prime numbers between 0 and a limit, but it writes 16 times (I have 16 cores and 16 processes) "Enter a limit: "我预期的 output 将是一个介于 0 和一个限制之间的素数列表,但它写入 16 次(我有 16 个内核和 16 个进程)“输入限制:”

Here is my code:这是我的代码:

import time
import os
from multiprocessing import Process

# Defining lists
primes = []
processes = []
l = [0]


limit = int(input('Enter a limit: '))

def primesearch(lower,upper):
    global primes
    for num in range(lower, upper):
        if num > 1:
            for i in range(2, num):
                if (num % i) == 0:
                    break
            else:
                primes.append(num)


# Start the clock
starter = time.perf_counter()

#Dividing data
step = limit // os.cpu_count()

for x in range(os.cpu_count()):
    l.append(step * (x+1))

l[-1] = limit

#Multiprocessing
for init in range(os.cpu_count()):
    processes.append(Process(target=primesearch, args=[l[init], l[init + 1],] ))

for process in processes:
    process.start()

for process in processes:
    process.join()


#End clock
finish = time.perf_counter()


print(primes)
print(f'Finished in {round(finish-starter, 2)} second')

What could be the problem?可能是什么问题呢?

You are using Windows - If you read the Python documenation for multiprocessing, it will reveal to you that you should protect your main code using if __name__==“__main__”: This is because on Windows each process re-executes the complete main.py file. You are using Windows - If you read the Python documenation for multiprocessing, it will reveal to you that you should protect your main code using if __name__==“__main__”: This is because on Windows each process re-executes the complete main.py文件。

This is used in pretty much every example in the documentation., and explained in the section at the end 'Programming guidelines'.这几乎在文档中的每个示例中都有使用,并在最后的“编程指南”部分中进行了解释。

See https://docs.python.org/3/library/multiprocessing.htmlhttps://docs.python.org/3/library/multiprocessing.html

Except for the __main__ issue, your way of using primes as a global list doesn't seem to work.除了__main__问题,您使用素数作为全局列表的方式似乎不起作用。 I imported Queue from multiprocessing and used primes = Queue() and我从多处理导入队列并使用primes = Queue()

    size = primes.qsize()
    print([primes.get() for _ in range(size)])
    primes.close()

in the main function and primes.put(num) in your function.在 function 中的主要 function 和primes.put(num)中。 I don't know if it's the best way, for me this works but if N > 12000 then the console freezes.我不知道这是否是最好的方法,对我来说这是可行的,但如果 N > 12000 则控制台会冻结。 Also, in this case, using multiprocessing is actually slightly slower than single process.此外,在这种情况下,使用多进程实际上比单进程稍慢。

If you aim for speed, you can test only to the square root of num, which saves about half of the time.如果你的目标是速度,你可以只测试 num 的平方根,这样可以节省大约一半的时间。 There are many optimizations you can do.您可以进行许多优化。 If you are testing huge numbers, you can use the Rabin-Miller algorithm.如果要测试大量数字,可以使用 Rabin-Miller 算法。 http://inventwithpython.com/cracking/chapter22.html http://inventwithpython.com/cracking/chapter22.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pyqt5 python 3.8 中的多处理共享内存 - multiprocessing shared memory in pyqt5 python 3.8 Python 3.8 多处理,用于在 MacOS 上复制随机数 - Python 3.8 multiprocessing for copying the random on MacOS 如何在 Python 3.8 的多处理中共享字符串数组? - How to share an Array of strings in multiprocessing in Python 3.8? python3.8下模块多处理错误 - error with module multiprocessing under python3.8 代码在 python 3.8 中不运行多处理功能 - Code does not run multiprocessing functions in python 3.8 Python 3.8 将 for 循环转换为多处理/多线程 - Python 3.8 Convert for loop to multiprocessing/multithreading python 3.8中异步内部的多处理失败(无法腌制本地对象) - multiprocessing inside async in python 3.8 fails (Can't pickle local object) Python 3.8 脚本在获取数据库连接(psycopg2 和多处理)时冻结 - Windows 7 - Python 3.8 script freezes on getting database connection (psycopg2 and multiprocessing) - Windows 7 为什么我不断收到 [BrokenPipeError: [Errno 32] Broken pipe],无论我的池中有多少工作人员在 python3.8 中使用多处理库? - Why do I keep getting [BrokenPipeError: [Errno 32] Broken pipe] no matter the number of workers in my Pool with multiprocessing lib in python3.8? Python 3.8 - 在 Windows 10 中执行 Python 3.8 - Python 3.8 - Execute Python 3.8 in Windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM