【问题标题】:Use 2 Threads to calculate the nth prime number in Python使用2个线程计算Python中的第n个素数
【发布时间】:2014-11-20 04:35:53
【问题描述】:

我正在尝试使用 Python 制作素数计算器。我设法编写了一个顺序版本,但我必须使其并行。

要求:

  1. 质数必须由函数重新计算,而不是 在某处被人仰视。

  2. 必须有 2 个线程参与计算

  3. 程序应该尽可能快


现在我的代码是:

import sys
import math 
import cProfile

def is_prime(num):
    for j in range(2,int(math.sqrt(num)+1)):
        if (num % j) == 0:
            return False
    return True

def prime(nth):
    """Return the nth prime number.
    >>> prime(3)
    The 3th prime number is: 5
    >>> prime(4)
    The 4th prime number is: 7
    >>> prime(1000)
    The 1000th prime number is: 7919
    """
    i = 0
    num = 2

    while i < nth:
        if is_prime(num): 
            i += 1
            if i == nth:
                print('The ' + str(nth) + 'th prime number is: ' + str(num))
        num += 1

if __name__ == "__main__":
    import doctest
    doctest.testmod()
    cProfile.run('print(prime(1000))')

【问题讨论】:

  • 您的代码看起来不错。问题是什么?我还认为,如果您只想找到 1000 的素数,那么 2. 和 3. 是互斥的。

标签: python multithreading performance primes


【解决方案1】:

这是我目前的答案,

import threading, doctest, cProfile, time, random
result = [2]
counter = 1

def get_int(num):
    for i in range(3, num):
        yield i

def is_prime(num):
    for j in range(2,int(num)):
        if (num % j) == 0:
            return False
    result.append(num)
    return True 

def prime_calculator(nth):
    lock = threading.Lock()
    global result, counter, integer
    while counter < (nth):
        if is_prime(next(integer)):
            lock.acquire()
            try:
                counter += 1
            finally:
                lock.release()
        time.sleep(random.random()/1000)

def prime(nth):
    """Returns the nth prime number
    >>> prime(1)
    2
    >>> prime(2)
    3
    >>> prime(4)
    7
    >>> prime(1000)
    7919
    """   
    global integer, counter, result
    integer = get_int(99999999)
    threads = [threading.Thread(daemon=True, target=prime_calculator, args=(nth,)) for i in range(2)]
    [thread.start() for thread in threads]
    [thread.join() for thread in threads]
    counter = 1
    return result[-1]

if __name__ == "__main__":
    doctest.testmod()
    cProfile.run('print(prime(1000))')

但是它不是线程安全的,因为它使用了一个计数器,稍后将尝试在没有计数器的情况下做一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2011-04-22
    • 2012-08-19
    • 2020-02-09
    • 2012-02-08
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多