Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Rework interface to update the batch in place instead of retur…
Browse files Browse the repository at this point in the history
…ning new instance
MatthieuMoreau0 committed Feb 13, 2024
1 parent ac4421a commit 457d0c5
Showing 2 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -124,10 +124,10 @@ Don't forget to mark your batch as closed/complete when you are done adding new
batch = sdk.create_batch(serialized_sequence, [job1], complete=False)
# Add some jobs to it and wait for the jobs to be terminated
job3 = {"runs": 50, "variables": {"omega_max": 10.5}}
batch = batch.add_jobs([job2, job3], wait=True)
batch.add_jobs([job2, job3], wait=True)
# When you have sent all the jobs to your batch, don't forget to mark it as complete
# Otherwise your batch will be timed out by the scheduler
batch = batch.declare_complete()
batch.declare_complete()
```

You can also choose to run your batch on an emulator using the optional argument `emulator`.
45 changes: 22 additions & 23 deletions pasqal_cloud/batch.py
Original file line number Diff line number Diff line change
@@ -124,7 +124,7 @@ def add_jobs(
self,
jobs: List[CreateJob],
wait: bool = False,
) -> "Batch":
) -> None:
"""Add some jobs to batch for execution on PCS and returns the updated batch.
The batch should not be `complete` otherwise the API will return an error.
@@ -140,21 +140,15 @@ def add_jobs(
except HTTPError as e:
raise JobCreationError(e) from e
batch = Batch(**batch_rsp, _client=self._client)
self.copy(batch, deep=True)
if wait:
while any(
[job.status in {"PENDING", "RUNNING"} for job in batch.ordered_jobs]
[job.status in {"PENDING", "RUNNING"} for job in self.ordered_jobs]
):
time.sleep(RESULT_POLLING_INTERVAL)
try:
batch_rsp = self._client._get_batch(self.id)
except HTTPError as e:
raise JobFetchingError(e) from e
batch = Batch(**batch_rsp, _client=self._client)
return batch

def declare_complete(
self, wait: bool = False, fetch_results: bool = False
) -> "Batch":
self.refresh()

def declare_complete(self, wait: bool = False, fetch_results: bool = False) -> None:
"""Declare to PCS that the batch is complete and returns an updated batch instance.
Args:
@@ -170,25 +164,30 @@ def declare_complete(
batch_rsp = self._client._complete_batch(self.id)
except HTTPError as e:
raise BatchSetCompleteError(e) from e
batch = Batch(**batch_rsp, _client=self._client)
complete_batch = Batch(**batch_rsp, _client=self._client)
self.copy(complete_batch, deep=True)
if wait or fetch_results:
while any(
[job.status in {"PENDING", "RUNNING"} for job in batch.ordered_jobs]
[job.status in {"PENDING", "RUNNING"} for job in self.ordered_jobs]
):
time.sleep(RESULT_POLLING_INTERVAL)
try:
batch_rsp = self._client._get_batch(self.id)
except HTTPError as e:
raise BatchFetchingError(e) from e
batch = Batch(**batch_rsp, _client=self._client)

return batch
self.refresh()

def cancel(self) -> Dict[str, Any]:
def cancel(self) -> None:
"""Cancel the current batch on the PCS."""
try:
batch_rsp = self._client._cancel_batch(self.id)
except HTTPError as e:
raise BatchCancellingError(e) from e
self.status = batch_rsp.get("status", "CANCELED")
canceled_batch = Batch(**batch_rsp, _client=self._client)
self.copy(canceled_batch)
return batch_rsp

def refresh(self) -> None:
"""Fetch the batch from the API and update it in place."""
try:
batch_rsp = self._client._get_batch(self.id)
except HTTPError as e:
raise BatchFetchingError(e) from e
updated_batch = Batch(**batch_rsp, _client=self._client)
self.copy(updated_batch, deep=True)

0 comments on commit 457d0c5

Please sign in to comment.