Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new multiprocessing context instead of using default context to avoid RuntimeError: context has already been set #3407

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions python/kserve/kserve/model_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import multiprocessing
import signal
import socket
from multiprocessing import Process
from typing import Dict, List, Optional, Union, Callable, Any

from ray import serve as rayserve
Expand Down Expand Up @@ -200,21 +199,21 @@ async def serve():
access_log_format=self.access_log_format)
await self._rest_server.run()
else:
# Since py38 MacOS/Windows defaults to use spawn for starting multiprocessing.
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
# Spawn does not work with FastAPI/uvicorn in multiprocessing mode, use fork for multiprocessing
# https://github.com/tiangolo/fastapi/issues/1586
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(('0.0.0.0', self.http_port))
serversocket.listen(5)
multiprocessing.set_start_method('fork')
self._rest_server = UvicornServer(self.http_port, [serversocket],
self.dataplane, self.model_repository_extension,
self.enable_docs_url, log_config=self.log_config,
access_log_format=self.access_log_format)
# Since py38 MacOS/Windows defaults to use spawn for starting multiprocessing.
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
# Spawn does not work with FastAPI/uvicorn in multiprocessing mode, use fork for multiprocessing
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, uvicorn internally uses 'spawn' for creating new subprocesses.

# https://github.com/tiangolo/fastapi/issues/1586
context = multiprocessing.get_context('fork')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this breaks the development on Mac?

for _ in range(self.workers):
p = Process(target=self._rest_server.run_sync)
p = context.Process(target=self._rest_server.run_sync)
p.start()

async def servers_task():
Expand Down
Loading