#Python multiprocessing project

9 messages · Page 1 of 1 (latest)

empty loom
#

Hellooo, im having trouble doing my python project for college.
So basically, i must get the biggest prime number within a certain time and with a certain number of processes.
The teacher gave us this functions, and told as that we can and should modify them:

def find_max_prime(timeout):
    """Finds the biggest prime until timeout."""
    max_prime = i = 1
    start = time.time()
    while time.time() - start < timeout:
        if is_prime(i) and i > max_prime:
            max_prime = i
        i += 1
    print(timeout, max_prime)

def is_prime(n):
    """Check if n is prime."""
    for i in range(2, n-1):
        if n % i == 0:
            return False
    return True

I tried to work it out with the multiprocessing.Pool() and Manager() but Im still confused, how can I make a certain number of processes program the same function within a time and give me a biggest number of all processes.
So for example, if I have 4 processes running this function "find_max_prime", all of them are gonna start at different numbers and whichever ends first the max prime first is gonna change a shared variable, and then starting coding the function again but after the highest prime found, so something like this,i would say:

def find_max_prime(timeout, number):
    """Finds the biggest prime until timeout."""
    max_prime = i = number
    start = time.time()
    while time.time() - start < timeout:
        if is_prime(i) and i > max_prime:
            max_prime = i
        i += 1
    print(timeout, max_prime)

I also tried using Lock() within this method but its my first time using it and im also having trouble with it:

def find_max_prime(timeout, number, lock):
    """Finds the biggest prime until timeout."""
    max_prime = i = number
    start = time.time()
    while time.time() - start < timeout:
        with lock:
            if is_prime(i) and i > max_prime:
                max_prime = i
            i += 1
    print(timeout, max_prime)
#

Im truly sorry if i didnt make myslef clear, english is not my main language.

hallow oar
#

here's a revised version of your code :

`import time
import multiprocessing

def find_max_prime_worker(number, timeout, shared_max_prime, lock):
"""Finds the biggest prime until timeout."""
max_prime = number
start = time.time()
while time.time() - start < timeout:
if is_prime(max_prime):
with lock:
if max_prime > shared_max_prime.value:
shared_max_prime.value = max_prime
max_prime += 1

def is_prime(n):
"""Check if n is prime."""
if n <= 1:
return False
if n == 2:
return True
if n % 2 == 0:
return False
i = 3
while i*i <= n:
if n % i == 0:
return False
i += 2
return True

def find_max_prime(timeout, num_processes):
# Using Manager to create a shared value
manager = multiprocessing.Manager()
shared_max_prime = manager.Value('i', 1)
lock = manager.Lock()

# Calculate the number of iterations for each process
iterations_per_process = timeout // num_processes

# Create a pool of processes
pool = multiprocessing.Pool(processes=num_processes)

# Start the processes
results = []
for i in range(num_processes):
    start_number = i * iterations_per_process + 1
    end_number = start_number + iterations_per_process
    result = pool.apply_async(find_max_prime_worker, (start_number, end_number, shared_max_prime, lock))
    results.append(result)

# Wait for all processes to finish
for result in results:
    result.get()

# Print the result
print(f"Max prime number found within {timeout} seconds is {shared_max_prime.value}")

if name == 'main':
timeout = 10 # Timeout in seconds
num_processes = 4 # Number of processes

find_max_prime(timeout, num_processes)`
#

if it works let me know

hallow oar
#

Do you have a problem

mighty dune
mighty dune
#

But that way of checking whether a number is prime will never be competetive