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

Multiprocessing spawn_main抛出OSError[WinError87]参数错误求助

Fixing OSError: [WinError 87] Invalid Parameter in Python Distributed Processes (Windows)

Hey there, let's work through this WinError 87 issue you're facing with the task_master.py code from the distributed processes tutorial. I've dealt with this exact problem on Windows before, so here are the most reliable fixes to get your code running:

1. Wrap Main Logic in if __name__ == '__main__' (Critical!)

Windows uses the spawn method to start multiprocessing processes by default, which re-runs your entire script when launching child processes. Without this guard clause, your queue setup and manager code will execute repeatedly, causing invalid parameter errors.

Update your code to look like this:

import multiprocessing
from multiprocessing.managers import BaseManager

# Define queues (outside the guard to avoid re-creation)
task_queue = multiprocessing.Queue()
result_queue = multiprocessing.Queue()

class QueueManager(BaseManager):
    pass

if __name__ == '__main__':
    # Register queues with the manager
    QueueManager.register('get_task_queue', callable=lambda: task_queue)
    QueueManager.register('get_result_queue', callable=lambda: result_queue)

    # Start the manager
    manager = QueueManager(address=('', 5000), authkey=b'abc')
    manager.start()

    # Access remote queues
    task = manager.get_task_queue()
    result = manager.get_result_queue()

    # Add tasks
    for i in range(10):
        print(f'Put task {i}...')
        task.put(i)

    # Retrieve results
    print('Waiting for results...')
    for i in range(10):
        r = result.get(timeout=10)
        print(f'Result: {r}')

    # Cleanup
    manager.shutdown()
    print('Master process exited.')

This guard ensures the manager and queue setup only runs once in the main process, preventing duplicate initialization that triggers the WinError 87.

2. Avoid Directly Passing Queue Objects

Never pass raw multiprocessing.Queue instances to child processes or expose them directly. Always use BaseManager to register and retrieve queues remotely, as shown above. Direct queue serialization on Windows often leads to parameter mismatch errors.

3. Upgrade to a Stable Python Version

Older Python versions (pre-3.7) have known bugs with the spawn multiprocessing method on Windows. If you're running an older release, upgrade to Python 3.7 or later—this alone fixes many obscure WinError issues in multiprocessing.

4. Check for Antivirus/Firewall Interference

Rarely, Windows Defender or third-party antivirus tools block inter-process communication ports used by multiprocessing. Try temporarily disabling your antivirus or adding Python to the trusted apps list to rule out this issue.

Start with the first fix (adding the __main__ guard)—it's the root cause 90% of the time for this specific error on Windows.

内容的提问来源于stack exchange,提问作者Raven 李

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:00:30