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

Fix multiprocessing to always use the spawn start method #16

Merged
merged 5 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions cogment_lab/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import abc
import copy
import logging
from typing import Awaitable, Callable, Generic, TypeVar
from typing import Awaitable, Callable, Dict, Generic, TypeVar

import cogment
import numpy as np
Expand All @@ -30,14 +30,14 @@


Action = TypeVar("Action")
Actions = dict[str, Action]
Actions = Dict[str, Action]

Observation = TypeVar("Observation")
Observations = dict[str, Observation]
Observations = Dict[str, Observation]

Rewards = dict[str, float]
Rewards = Dict[str, float]

Dones = dict[str, bool]
Dones = Dict[str, bool]


class State:
Expand Down
2 changes: 2 additions & 0 deletions cogment_lab/envs/pettingzoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import logging
from typing import Any, TypedDict

Expand Down
10 changes: 7 additions & 3 deletions cogment_lab/process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
user_id: str = "cogment_lab",
torch_mode: bool = False,
log_dir: str | None = None,
mp_method: str | None = None,
mp_method: str = "spawn",
orchestrator_port: int = 9000,
datastore_port: int = 9003,
):
Expand All @@ -67,8 +67,12 @@ def __init__(
user_id (str, optional): User ID. Defaults to "cogment_lab".
torch_mode (bool, optional): Whether to use PyTorch multiprocessing. Defaults to False.
log_dir (str, optional): Directory to store logs. Defaults to "logs".
mp_method (str | None, optional): Multiprocessing method to use. Defaults to None.
mp_method (str, optional): Multiprocessing method to use. Defaults to "spawn". WARNING: Methods other than spawn are likely to not work. Use at your own risk.
"""
try:
mp.set_start_method(mp_method)
except RuntimeError:
pass
self.processes: dict[ImplName, Process] = {}
self.tasks: dict[ImplName, Task] = {}

Expand Down Expand Up @@ -132,7 +136,7 @@ def _add_process(

p = TorchProcess(target=target, args=args)
else:
p = self.mp_ctx.Process(target=target, args=args) # type: ignore
p = self.mp_ctx.Process(target=target, args=args, name=f"cogment_lab_{name}") # type: ignore
p.start()
self.processes[name] = p

Expand Down
2 changes: 2 additions & 0 deletions cogment_lab/specs/action_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import gymnasium as gym

from cogment_lab.generated.data_pb2 import PlayerAction # type: ignore
Expand Down
Loading