Skip to content

Commit

Permalink
Use execnet main_thread_only execmodel
Browse files Browse the repository at this point in the history
Use the execnet main_thread_only execmodel so that code which expects
to run in the main thread will just work.  This execmodel has been
proposed in pytest-dev/execnet#243, so this
patch should not be merged until there is a release version of execnet
supporting the main_thread_only execmodel.

Closes: pytest-dev#620
  • Loading branch information
zmedico committed Feb 16, 2024
1 parent 6ae08a0 commit 640ee7a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/xdist/looponfail.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def trace(self, *args):
print("RemoteControl:", msg)

def initgateway(self):
return execnet.makegateway("popen")
return execnet.makegateway("execmodel=main_thread_only//popen")

def setup(self, out=None):
if out is None:
Expand Down
8 changes: 7 additions & 1 deletion src/xdist/workermanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ def __init__(self, config, specs=None, defaultchdir="pyexecnetcache") -> None:
self.testrunuid = self.config.getoption("testrunuid")
if self.testrunuid is None:
self.testrunuid = uuid.uuid4().hex
self.group = execnet.Group()
self.group = execnet.Group(execmodel="main_thread_only")
if specs is None:
specs = self._getxspecs()
self.specs = []
for spec in specs:
if not isinstance(spec, execnet.XSpec):
spec = execnet.XSpec(spec)
if getattr(spec, "execmodel", None) != "main_thread_only":
spec = execnet.XSpec(f"execmodel=main_thread_only//{spec}")
if not spec.chdir and not spec.popen:
spec.chdir = defaultchdir
self.group.allocate_id(spec)
Expand All @@ -68,6 +70,10 @@ def setup_nodes(self, putevent):
return [self.setup_node(spec, putevent) for spec in self.specs]

def setup_node(self, spec, putevent):
if not isinstance(spec, execnet.XSpec):
spec = execnet.XSpec(spec)
if getattr(spec, "execmodel", None) != "main_thread_only":
spec = execnet.XSpec(f"execmodel=main_thread_only//{spec}")
gw = self.group.makegateway(spec)
self.config.hook.pytest_xdist_newgateway(gateway=gw)
self.rsync_roots(gw)
Expand Down
2 changes: 1 addition & 1 deletion testing/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, request, pytester: pytest.Pytester) -> None:
def setup(self) -> None:
self.pytester.chdir()
# import os ; os.environ['EXECNET_DEBUG'] = "2"
self.gateway = execnet.makegateway()
self.gateway = execnet.makegateway("execmodel=main_thread_only")
self.config = config = self.pytester.parseconfigure()
putevent = self.events.put if self.use_callback else None

Expand Down
2 changes: 1 addition & 1 deletion testing/test_workermanage.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_hrsync_filter(self, source: Path, dest: Path) -> None:
assert names == {"dir", "file.txt", "somedir"}

def test_hrsync_one_host(self, source: Path, dest: Path) -> None:
gw = execnet.makegateway("popen//chdir=%s" % dest)
gw = execnet.makegateway("execmodel=main_thread_only//popen//chdir=%s" % dest)
finished = []
rsync = HostRSync(source)
rsync.add_target_host(gw, finished=lambda: finished.append(1))
Expand Down

0 comments on commit 640ee7a

Please sign in to comment.