Skip to content

Commit

Permalink
Merge pull request #1301 from pyiron/wait_for_job
Browse files Browse the repository at this point in the history
Fix wait_for_job() when for jobs with executors
  • Loading branch information
jan-janssen authored Jan 31, 2024
2 parents 030a631 + 1142560 commit 22d2aa8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pyiron_base/jobs/job/extension/server/queuestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,13 @@ def wait_for_job(job, interval_in_s=5, max_iterations=100):
finished = True
break
elif isinstance(job.server.future, Future):
job.server.future.result(timeout=interval_in_s)
finished = job.server.future.done()
break
try:
job.server.future.result(timeout=interval_in_s)
except TimeoutError:
pass
else:
finished = job.server.future.done()
break
else:
time.sleep(interval_in_s)
if not finished:
Expand Down
34 changes: 34 additions & 0 deletions tests/flex/test_pythonfunctioncontainer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import unittest
from concurrent.futures import ProcessPoolExecutor
import sys
from time import sleep
from pyiron_base._tests import TestWithProject


def my_function(a, b=8):
return a+b


def my_sleep_funct(a, b=8):
sleep(0.01)
return a+b


class TestPythonFunctionContainer(TestWithProject):
def test_as_job(self):
job = self.project.wrap_python_function(my_function)
Expand All @@ -27,3 +36,28 @@ def test_as_function(self):
self.assertEqual(job_reload.input["a"], 5)
self.assertEqual(job_reload.input["b"], 6)
self.assertEqual(job_reload.output["result"], 11)

def test_with_executor(self):
with ProcessPoolExecutor() as exe:
job = self.project.wrap_python_function(my_sleep_funct)
job.input["a"] = 4
job.input["b"] = 5
job.server.executor = exe
self.assertTrue(job.server.run_mode.executor)
job.run()
self.assertFalse(job.server.future.done())
self.assertIsNone(job.server.future.result())
self.assertTrue(job.server.future.done())

@unittest.skipIf(sys.version_info < (3, 11), reason="requires python3.11 or higher")
def test_with_executor_wait(self):
with ProcessPoolExecutor() as exe:
job = self.project.wrap_python_function(my_sleep_funct)
job.input["a"] = 4
job.input["b"] = 6
job.server.executor = exe
self.assertTrue(job.server.run_mode.executor)
job.run()
self.assertFalse(job.server.future.done())
self.project.wait_for_job(job=job, interval_in_s=0.01, max_iterations=1000)
self.assertTrue(job.server.future.done())

0 comments on commit 22d2aa8

Please sign in to comment.