如何不修改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, theexec()call runs all code froma.pyinsideb.py's environment. - Since we're executing this in
b.py's main block,__name__will equal'__main__', triggeringa.py's main logic. - The
taskandargsreferenced ina.py'sp.map()call will resolve to the ones defined inb.py.
Notes:
- Ensure
a.pyis in the same directory asb.py, or adjust the file path inopen()to point to its correct location. - This works with multiprocessing because your
taskis 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.pyas a module, then overwrite itstaskandargsattributes with our own definitions. - We then run the exact same pool/map code that exists in
a.py's main block, using the modifiedamodule'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 ifa.pyis frequently updated.
内容的提问来源于stack exchange,提问作者tianya zhang
相关产品推荐
相关产品推荐

