You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不修改a.py将b.py的task与args传入其main函数实现并行编程

Solution: Pass Functions/Args to Another Script's Main Without Modifying It

Great question! Since you can't modify a.py, we need to work around its structure to make it use the task function and args parameter from b.py. Here are two practical, reliable approaches:

Approach 1: Execute a.py in b.py's Namespace

This method runs the entire content of a.py directly within b.py's context, so a.py will automatically use your task and args without any changes to its code.

Update your b.py to include this at the bottom:

# b.py
import multiprocessing

def task(x):
    return x*x

args = range(10)

if __name__ == '__main__':
    # Read and execute a.py's code in the current namespace
    with open('a.py', 'r') as f:
        a_script_content = f.read()
    exec(a_script_content)

How it works:

  • When you run python b.py, the exec() call runs all code from a.py inside b.py's environment.
  • Since we're executing this in b.py's main block, __name__ will equal '__main__', triggering a.py's main logic.
  • The task and args referenced in a.py's p.map() call will resolve to the ones defined in b.py.

Notes:

  • Ensure a.py is in the same directory as b.py, or adjust the file path in open() to point to its correct location.
  • This works with multiprocessing because your task is a top-level function (picklable, which is required for cross-process communication on Windows).

Approach 2: Inject Variables into a.py's Module Namespace

If you prefer not to execute the entire a.py file, you can import it as a module, override its task and args, then replicate its main logic.

Modify b.py like this:

# b.py
import multiprocessing
import a

def task(x):
    return x*x

args = range(10)

if __name__ == '__main__':
    # Replace the task and args in the imported a module
    a.task = task
    a.args = args
    
    # Run the same multiprocessing logic from a.py's main block
    p = multiprocessing.Pool(4)
    results = p.map(a.task, a.args)
    print(results)

How it works:

  • We import a.py as a module, then overwrite its task and args attributes with our own definitions.
  • We then run the exact same pool/map code that exists in a.py's main block, using the modified a module's variables.

Notes:

  • The downside here is that if a.py's main logic ever changes (e.g., pool size, additional steps), you'll need to update this code to match. It's more explicit but less maintainable if a.py is frequently updated.

内容的提问来源于stack exchange,提问作者tianya zhang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:59:54