Skip to content
Merged
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
32 changes: 25 additions & 7 deletions perceval_interop/myqlm/myqlm_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from __future__ import annotations

import time
from typing import Any

from qat.core.qpu import RemoteQPU
Expand Down Expand Up @@ -75,11 +76,11 @@ def create_job(self, payload) -> str:
my_qlm_job = MyQLMHelper.make_job_from_payload(payload)
job_id = str(self._job_id)
self._job_id += 1
# TODO: make the job execution async ?
res = self.remote_qpu.submit(my_qlm_job)
if isinstance(res, AsyncResult):
res = res.join()
self._job_results[job_id] = {"results": res.meta_data[MyQLMHelper.RESULTS_KEY]}
self._job_results[job_id] = res
else:
self._job_results[job_id] = {"results": res.meta_data[MyQLMHelper.RESULTS_KEY]}
return job_id

def get_job_results(self, job_id: str) -> dict:
Expand All @@ -90,22 +91,39 @@ def get_job_results(self, job_id: str) -> dict:
"""
if job_id not in self._job_results:
raise RuntimeError("The given job id does not exist")
return self._job_results[job_id]
res = self._job_results[job_id]
if isinstance(res, AsyncResult):
while res.ending_date is None:
time.sleep(1)
res = {"results": res.meta_data[MyQLMHelper.RESULTS_KEY]}
return res

def get_job_status(self, job_id: str) -> dict:
"""get the status of a job

:param job_id: if of the job
:return: status of the job
"""
status = "completed" if job_id in self._job_results else "waiting" # Actually, else not sent, but this status doesn't exist
res = self._job_results.get(job_id)
if res is None:
raise RuntimeError("The given job id does not exist")
if isinstance(res, AsyncResult):
status = "completed" if res.ending_date is not None else "waiting"
else:
status = "completed" if job_id in self._job_results else "waiting" # Actually, else not sent, but this status doesn't exist
return {"status": status}

def rerun_job(self, job_id: str) -> str:
raise NotImplementedError("rerun_job is not implemented for MyQLM Remote Processors")

def cancel_job(self, job_id: str) -> None:
raise NotImplementedError("cancel_job is not implemented for MyQLM Remote Processors")
res = self._job_results.get(job_id)
if res is None:
raise RuntimeError("The given job id does not exist")
if isinstance(res, AsyncResult):
res.cancel()
else:
raise NotImplementedError("cancel_job is not implemented for MyQLM Remote Processors")


class MyQLMSession(ISession):
Expand All @@ -116,7 +134,7 @@ class MyQLMSession(ISession):

- Being used in a JobGroup
- Retrieving a job from its id (so a job is lost forever if python stops)
- Some advanced job methods (cancel, rerun...)
- Some advanced job methods (rerun...)
- Jobs will always be executed synchronously

Usage:
Expand Down
Loading