Job Progress, Cancellation and Limits

by Giux GX-TXT, python-en, scripts-en 3 min read

Scientific Python in GX-TXT · Module 7 · Lesson 41

A long operation should communicate what it is doing without unnecessarily blocking the interface. In this lesson we elaborate a small synthetic series in blocks and publish progress events proportioned to actual work.

Objectives

  • Representing determined advancement with work done and total.
  • Distinguish startup, work, completion and error.
  • Consult the parallel resource policy.
  • Understand that request for cancellation and cancellation ended are distinct events.

Run the script

In the corso_python Session, import job_progress.py, save it and start it with Run → Open Script Runner → Run Job.

Job progress script in the GX-TXT editor.
Job progress script in the GX-TXT editor.
import numpy as np
from gxtxt import gxtxt_api_call, gxtxt_api_require

for api in ("PROGRESS_API_v1@1", "CANONICAL_DATASET_API_v2@2", "PARALLEL_EXECUTION_API_v1@1"):
    gxtxt_api_require(api)
progress = "PROGRESS_API_v1"
def report(state, current, overall_total):
    event = {"state": state, "mode": "determinate", "stage": "lettura", "message": "Synthetic data reading", "current": current, "total": overall_total}
    return gxtxt_api_call(progress, "publish", [event])
report("start", 0, 4)
total = 0.0
try:
    for start in (0, 2):
        chunk = gxtxt_api_call("CANONICAL_DATASET_API_v2", "read_range", ["corso_python_serie_sintetica", ["segnale"], start, 2])
        total += float(np.sum(chunk["segnale"]["values"]))
        report("running", start + 2, 4)
    report("completed", 4, 4)
except Exception:
    report("failed", 0, 4)
    raise
print("Samples processed:", 4)
print("Synthetic average (mV):", total / 4)
print("Resources available:", gxtxt_api_call("PARALLEL_EXECUTION_API_v1", "resources", []))

The report function builds an event with state, phase, message, current and total quantity. We really read two blocks of two lines: after each reading we communicate the actual number of samples processed. completed is sent only after work; in case of exception we send failed and we raise the error. We do not introduce artificial pauses: on four rows progress can be too fast to be observed in real time.

Job output showing four samples and a synthetic mean of 1.4 mV.
Job output showing four samples and a synthetic mean of 1.4 mV.

The result is the average of the four synthetic values. The resources response describes the policy available in the execution environment: in the image the limit is a worker. It is an information about the context, not a request to start parallel processes.

States and deletion

Certain progress is appropriate when current and total are measurable work. If the total is not known, an indefinite indicator is more honest than an invented percentage. Aver reached the total and successfully concluded are distinct concepts: a last phase can still fail.

In the Job Manager, the request for cancellation is not immediately equivalent to the confirmation that the process and its resources have been closed. Check the final status of Job and use the erasure functions available for the concrete operation. This short example has been completed; it does not show a successful cancellation. For work with managed resources, always predict appropriate cleaning even in case of error.

Exercise

  1. What values of current are communicated during reading?
  2. Why don’t we put a break between the two blocks?
  3. Did a Job come to four out of four samples necessarily complete each phase?

Worked solution

  1. After starting at 0, the work communicates 2 and 4 samples processed.
  2. A break would not represent real work; the example should not slow down just to make the bar visible.
  3. No. The successful closure is a separate state and can depend on other operations.

In the next lesson we will compare repeated results and rescue.

Download the Module 7 scripts (ZIP)

#GX-TXT #Python #Reproducibility

Other steps of this project

Leave a Reply

Your email address will not be published. Required fields are marked *