Skip to content

Commit

Permalink
Merge pull request #73 from mraspaud/fix_get_context
Browse files Browse the repository at this point in the history
Reintroduce global get_context
  • Loading branch information
pnuu authored Feb 5, 2025
2 parents 8bb9100 + 3da1981 commit 6285120
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions posttroll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,26 @@
"""Posttroll packages."""

import logging
from warnings import warn

from donfig import Config

config = Config("posttroll", defaults=[dict(backend="unsecure_zmq")])

logger = logging.getLogger(__name__)


def get_context():
"""Provide the context to use.
This function takes care of creating new contexts in case of forks.
"""
warn("Posttroll's get_context function is deprecated. If you really need it, import the corresponding backend's"
" get_context instead",
stacklevel=2)
backend = config["backend"]
if "zmq" in backend:
from posttroll.backends.zmq import get_context
return get_context()
else:
raise NotImplementedError(f"No support for backend {backend} implemented (yet?).")
19 changes: 19 additions & 0 deletions posttroll/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Module for common pytest fixtures."""

import pytest
import zmq

import posttroll.backends.zmq.socket


@pytest.fixture(autouse=True)
def new_context(monkeypatch):
"""Create a new context for each test."""
context = zmq.Context()
def get_context():
return context
monkeypatch.setattr(posttroll.backends.zmq.socket, "get_context", get_context)
yield
context.term()


0 comments on commit 6285120

Please sign in to comment.