共享在Python多处理中实时更新的数据

发布于 2025-02-05 17:19:20 字数 2898 浏览 0 评论 0原文

我试图帮助某人做作业。目的是尝试进行多个程序,并帮助他们优化它。由于这些函数通过不断地实时修改的数据修改工作,因此他认为制作变量会有所帮助,但是由于多处理的工作方式,它并没有进行多处理。

问题是我对共享内存多处理不了解。我知道地图池的内容如何创建过程以及所有这些,但是使它们更新并同时依赖相同的信息对我来说有点棘手。我已经知道我需要做什么以及将为代码编写的优化。我只想知道如何解决这个特定问题以及其中的任何建议。

编辑以进行一些澄清和TLDR(也稍微删除代码):

代码摘要: 因此,问题是该算法的作用如下:

  1. 以下数字
  2. 通过质量数列表进行分解n,直到
  3. 每次prime检查n,如果n分为n
  4. ,则计算出n,请计数多少次,并添加在保存该部门的总结果的因素列表中
  5. ,将质数的目标重置为迭代,以直到该部门结果的SQRT

,这是困难的部分,我该如何使其这样这些信息在所有多过程中都可以准确地更新?

# PRIME FACTORIZATION
import time
import numpy as np
import math


def in_time_target_updater(new_divisor):
    global last_divisor
    global new_upto_target
    global full_number_target

    full_number_target = new_divisor
    last_divisor = new_divisor
    new_upto_target = math.sqrt(new_divisor)

def exponent_finder(num, input_target):
    """Finds how many times a prime goes into a number. For each time the number divides into a int it appends
    the number into the factors list. Once that is done it returns the last divisor in order to do updates.
    """
    global factor_list
    other_divisor = input_target
    for i in range(max_range):
        local_target = input_target//(num**(i+1))
        if other_divisor % num == 0:
            factor_list = np.append(factor_list, num)
            other_divisor = local_target
            continue
        else:
            return other_divisor

def phase1_list_iterator(list_or_queue):
    global found
    for value in list_or_queue:
        just_in_time_iteration(value)
        if found:
            break

def just_in_time_iteration(value):
    global found
    global factor_list

    if value > new_upto_target:

        found = True
        factor_list = np.append(factor_list, last_divisor)
        return found
    else:
        if full_number_target % value == 0:
            remaining_divisor = exponent_finder(value, full_number_target)
            in_time_target_updater(remaining_divisor)

def phase1():
    global factor_list
    phase1_list_iterator(prime_array)
    if found is True:
        if factor_list[-1] == 1:
            factor_list = factor_list[:-1]
        if factor_list[0] == 0:
            factor_list = np.array([num_to_factorize])
        return
    else:
         phase2()

def main_single_thread_comparative(input_):
    global num_to_factorize
    global max_range
    global og_target
    global new_upto_target
    global full_number_target
    global last_divisor
    global factor_list
    global prime_array
    global found
    global step_constant

    num_to_factorize = input_
    max_range = 200
    og_target = math.sqrt(num_to_factorize)
    new_upto_target = og_target
    full_number_target = num_to_factorize
    last_divisor = 0
    factor_list = np.empty(0, dtype=int)
    prime_array = np.load('prime_10000.npy')
    found = False
    step_constant = 2


    phase1()

I was trying to help someone with their homework. The goal is to try to multiprocess this program and help them optimize it. Because the functions modify work with data thats constantly being modified in real time, he thought making the variables would help, but it doesnt in multiprocessing because of how multiprocessing works.

The issue is though I dont know much about shared memory multiprocessing. I know how map pool stuff create process and all that, however making them update and rely on the same information concurrently is a bit trickier for me. I already know what I need to do and optimizations I am going to write for the code. I just want to know about how I can go about fixing this specific problem, and any suggestions therein.

Edit for some clarification and tldr (also cut down the code a bit):

Summary of code:
So the issue is the algorithm works as follow:

  1. take a number to factorize n
  2. iterate through a list of prime numbers up to the sqrt of n
  3. for each prime check if n divides into it
  4. if it divides into it, count how many times and add that to a list of factors
  5. save the total result of the division, reset the target of prime numbers to iterate through to be only up to the sqrt of the result of this division

This number 5 is the hard part, how do I make it such that this information is updated across all multi-processes accurately?

# PRIME FACTORIZATION
import time
import numpy as np
import math


def in_time_target_updater(new_divisor):
    global last_divisor
    global new_upto_target
    global full_number_target

    full_number_target = new_divisor
    last_divisor = new_divisor
    new_upto_target = math.sqrt(new_divisor)

def exponent_finder(num, input_target):
    """Finds how many times a prime goes into a number. For each time the number divides into a int it appends
    the number into the factors list. Once that is done it returns the last divisor in order to do updates.
    """
    global factor_list
    other_divisor = input_target
    for i in range(max_range):
        local_target = input_target//(num**(i+1))
        if other_divisor % num == 0:
            factor_list = np.append(factor_list, num)
            other_divisor = local_target
            continue
        else:
            return other_divisor

def phase1_list_iterator(list_or_queue):
    global found
    for value in list_or_queue:
        just_in_time_iteration(value)
        if found:
            break

def just_in_time_iteration(value):
    global found
    global factor_list

    if value > new_upto_target:

        found = True
        factor_list = np.append(factor_list, last_divisor)
        return found
    else:
        if full_number_target % value == 0:
            remaining_divisor = exponent_finder(value, full_number_target)
            in_time_target_updater(remaining_divisor)

def phase1():
    global factor_list
    phase1_list_iterator(prime_array)
    if found is True:
        if factor_list[-1] == 1:
            factor_list = factor_list[:-1]
        if factor_list[0] == 0:
            factor_list = np.array([num_to_factorize])
        return
    else:
         phase2()

def main_single_thread_comparative(input_):
    global num_to_factorize
    global max_range
    global og_target
    global new_upto_target
    global full_number_target
    global last_divisor
    global factor_list
    global prime_array
    global found
    global step_constant

    num_to_factorize = input_
    max_range = 200
    og_target = math.sqrt(num_to_factorize)
    new_upto_target = og_target
    full_number_target = num_to_factorize
    last_divisor = 0
    factor_list = np.empty(0, dtype=int)
    prime_array = np.load('prime_10000.npy')
    found = False
    step_constant = 2


    phase1()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文