Skip to content

Long-running operations

Some Microsoft 365 calls don't finish in one round-trip. Microsoft Graph answers them with 202 Accepted and a header pointing at an operation-status (monitor) URL; you poll that URL until the work reaches a terminal status, then read the result. This is the Graph async pattern, and it is what copying a large Drive item, cloning a team, or recalculating a workbook actually do:

sequenceDiagram
    participant C as Client
    participant G as Graph
    C->>G: POST /copy  (request)
    G-->>C: 202 Accepted + Location: .../operations/op1
    loop until terminal
        C->>G: GET .../operations/op1
        G-->>C: 200 {status: inProgress | completed, percentageComplete}
    end
    C->>G: GET resourceId / resourceLocation

The library gives you three levels of help, all built on the same transport-agnostic poller (office365.runtime.lro):

  1. Waitable results — driveItem.copy() returns a result whose wait() / wait_async() poll to completion and hand back the new item.
  2. Operation entities — TeamsAsyncOperation, WorkbookOperation, and rich long-running operations are entities you get() and poll, generically with wait() / wait_async() (and poll_for_status() on TeamsAsyncOperation, which waits for a named status rather than any terminal one).
  3. The raw poller — OperationPoller, for anything else, with pluggable URL resolution, Retry-After pacing, progress callbacks and continuation tokens.

All three are fully awaitable: wait_async() yields to the event loop between polls, so it never blocks concurrent tasks. See Async / await for the async model itself.

Waitable results

The most ergonomic path. DriveItem.copy() returns a DriveItemCopyResult (a LongRunningOperationResult); execute_query() submits the copy and captures the monitor URL from the Location header, then wait_for_item() / wait_for_item_async() poll and resolve the new item:

source = client.me.drive.root.get_by_path("Reports/big.xlsx").get().execute_query()
result = source.copy(name="big (copy).xlsx", parent=dest).execute_query()

status = result.wait()  # blocking; or: status = await result.wait_async()

# The result is a LongRunningOperationResult: inspect or resolve it yourself
print(result.monitor_url, result.resource_id, result.resource_location)

# Or let the wrapper load the produced DriveItem for you
copied = result.wait_for_item()  # / await result.wait_for_item_async()
print(copied.web_url)
Method Purpose
result.wait(...) / wait_async(...) Poll to terminal, return the OperationStatus
result.wait_for_item() / wait_for_item_async() Poll, then load and return the produced DriveItem
result.monitor_url The operation-status URL captured from Location (None for work that finished in one round-trip)
result.last_status The most recent OperationStatus snapshot
result.resource_id / result.resource_location The produced resource, once reported

LongRunningOperationResult attaches credentials when polling by default, because Graph hands back authenticated monitor URLs (often on the SharePoint host) for these actions. The raw OperationPoller does not — see below.

Operation entities

When the service models the operation as a resource (rather than only a monitor URL), the entity itself is pollable. PollableOperation supplies wait() / wait_async(), stream() / astream(), cancel() / cancel_async(), operation_status, is_operation_complete() / is_operation_failed(), next_poll_delay() and is_transient_poll_error(); the concrete classes add typed properties — and generated operation entities (LongRunningOperation, security.CaseOperation, PrintOperation, … and their subtypes) get the mixin automatically.

wait() returns the entity, refreshed; stream() yields the same snapshots the polling loop sees, so a progress bar can follow percentageComplete without giving up the entity:

operation = client.sites[site_id].operations["op-id"].get().execute_query()
for status in operation.stream():  # async twin: async for status in operation.astream()
    print(status.status, status.percentage_complete)
print(operation.resource_location)

A transient 404 — the entity a service publishes a moment after accepting the work — is an attempt, not an observation: the wait retries it at next_poll_delay() and the progress hook is not told that anything happened. An entity has no generic cancel request, so cancel() stops waiting only; a service that can abandon the work exposes its own method.

Teams — clone() / create() return a TeamsAsyncOperation; execute_query() submits it (HTTP 202), then poll:

from office365.teams.clonableteamparts import ClonableTeamParts
from office365.teams.visibility_type import TeamVisibilityType

operation = target_team.clone(
    mail_nickname="clone1",
    display_name="Project Falcon",
    parts_to_clone=ClonableTeamParts.settings,
    visibility=TeamVisibilityType.private,
).execute_query()

await operation.poll_for_status_async(timeout_sec=300, polling_interval=15)
cloned_id = operation.target_resource_id

# Or the one-shot convenience that returns the ready team
cloned = target_team.clone_and_wait(
    mail_nickname="clone1",
    display_name="Project Falcon",
    parts_to_clone=ClonableTeamParts.settings,
    visibility=TeamVisibilityType.private,
).execute_query()

poll_for_status() is the same wait with a status_type argument: it ends at the status you named (default succeeded) or at one that says it will never arrive (failed), and fires exactly one callback — success, or failure for a failed status or the deadline. It polls to completion itself rather than queueing a chain of callbacks, so it works both on its own and from inside an after_execute hook, which is how create_and_wait() / clone_and_wait() chain submit → poll → reload in a single execute_query(). A transient 404/429/503 is "not ready yet"; the deadline is read from the clock and the pause honours Retry-After.

Workbook — workbook.operations is an EntityCollection[WorkbookOperation]; after a respond-async submission the operation is pollable:

operation = workbook.operations["op-id"].get().execute_query()
operation.wait(interval=2, timeout=300)
print(operation.status, operation.resource_location, operation.error)

Rich long-running operations — client.sites[site_id].operations[...] exposes Graph's RichLongRunningOperation (same PollableOperation surface), used by site-level work.

The raw poller

OperationPoller works with any 202 response, from either client. Build it from the response, a known URL, or a continuation token:

from office365.runtime.lro import OperationPoller, resolve_poll_url

response = ...  # a 202 Accepted response, however you obtained it
poller = OperationPoller.from_response(context, response, interval=5, timeout=1800)

status = poller.wait(on_progress=lambda s: print(s.status, s.percentage_complete))
# or, without blocking the loop:
status = await poller.wait_async(on_progress=...)

if status.is_success:
    print(status.resource_id, status.resource_location)

wait() is stream() with the failure policy applied: when a callback is not enough — a progress bar that wants the snapshot, a UI that shows a stall, a worker that logs each step — iterate instead:

for status in poller.stream():  # async twin: async for status in poller.astream()
    print(status.status, status.percentage_complete, status.last_action_date_time)

# or, from a waitable result (keeps result.last_status current):
for status in result.stream():
    ...

A stream yields every snapshot, the terminal one last, and raises nothing for a failed or cancelled outcome — the caller sees it. Breaking out of the loop stops polling. Waitable results have the same pair.

Argument Meaning
interval Seconds between polls when the server sends no Retry-After (default 5)
timeout Maximum seconds to wait before OperationTimeoutError (default 1800)
authenticate Attach the context's credentials to each poll. Default False: Graph monitor URLs are unauthenticated and may live on another host (api.onedrive.com). Pass True for services such as Azure that require it
on_progress Called with every OperationStatus snapshot
cancel_request Optional request that cancels the operation server-side, sent by cancel() / cancel_async()
transport / async_transport Override the transport used by wait() / wait_async()
clock / sleep / async_sleep Injectable timing hooks (tests). By default the poller waits on an interruptible event, so cancel() wakes a waiting wait() / wait_async() immediately

The poller never sleeps past the deadline: if the next delay would exceed timeout, it raises OperationTimeoutError(timeout) instead of waiting. It honors Retry-After and backs off on 429/503.

Poll-URL resolution

resolve_poll_url(response, final_state_via=...) picks the monitor URL from the response, in order, across POLL_URL_HEADERS = ("Operation-Location", "Azure-AsyncOperation", "Location"):

from office365.runtime.lro import POLL_URL_HEADERS, resolve_poll_url

url = resolve_poll_url(response)  # "auto": first header present
url = resolve_poll_url(response, final_state_via="location")  # a specific header
url = resolve_poll_url(response, final_state_via="original-url", original_url=request.url)

OperationPoller.from_response raises ValueError when no monitor URL header is present — i.e. the response was not an accepted async operation.

Status vocabulary

OperationStatus is a dataclass snapshot: status, http_status, percentage_complete, resource_id, resource_location, error, the raw response, plus the operation's own timeline — created_date_time, last_action_date_time (both timezone-aware, so now - last_action_date_time is an honest age) and retry_after (what this response asked the client to wait). Its is_success / is_failure / is_terminal / is_cancelled / is_partially_succeeded predicates normalize the service's wording, so Graph (completed) and Azure (Succeeded) both work:

from office365.runtime.lro import CANCELLED_STATUSES, FAILURE_STATUSES, PARTIAL_STATUSES, SUCCESS_STATUSES

SUCCESS_STATUSES  # {"completed", "succeeded", "success", "finished"}
FAILURE_STATUSES  # {"failed", "failure", "cancelled", "canceled", "error"}
CANCELLED_STATUSES  # {"cancelled", "canceled"}
PARTIAL_STATUSES  # {"partiallysucceeded", "partiallysuccessful", "partiallycompleted"}
  • A 3xx (redirect to the final result) is terminal.
  • A terminal failure raises OperationFailedError(status) from wait() unless you pass raise_on_failure=False.
  • A cancelled status also counts as a failure (for backward compatibility); use status.is_cancelled to tell the two apart.
  • OperationCancelledError (a subclass of OperationFailedError) is raised for a cancelled terminal status; OperationError is the base of all three.
  • A partially succeeded operation is terminal but neither a success nor a failure: wait() returns it instead of raising, and status.is_partially_succeeded says so. SharePoint copy and migration jobs report item-level failures this way.
  • Any other word is "keep polling" — so a service with its own vocabulary needs a status it recognizes, or the wait runs to the timeout.

Cancellation

OperationPoller.cancel() (async: cancel_async()) requests cancellation. It sets a flag and wakes any in-flight wait() / wait_async() — typically running in another thread or task — immediately, where the wait raises OperationCancelledError. A wait() started after cancellation returns immediately. Even a long Retry-After does not delay cancellation. (Passing a custom sleep / async_sleep takes over the wait, and then cancellation is honored only when that callable returns.)

poller = OperationPoller.from_response(context, response)

# Cancel from another thread while a blocking wait is in progress:
poller.cancel()

# Or have a cancelled task also stop the operation server-side:
try:
    await poller.wait_async(cancel_on_task_cancel=True)
except asyncio.CancelledError:
    ...

Cancellation semantics:

  • It is cooperative, not a rollback: work the service already applied stays.
  • It only reaches the service when a cancel_request was configured (a service-specific call such as a DELETE on a Graph operation resource). Without it, cancel() stops waiting only and the operation keeps running.
  • wait_async(..., cancel_on_task_cancel=True) forwards a task cancellation to the service before the asyncio.CancelledError propagates; the default (False) cancels the wait only.
  • Cancellation is prompt: a blocking wait() running in another thread is woken immediately, and a running wait_async() is woken on its event loop. A synthetic OperationStatus(status="cancelled") is reported.

The engine behind the poller

OperationPoller is office365.runtime.operation.Operation specialised to one kind of poll: a GET of a monitor URL. PollableOperation is the same base specialised to another: re-read the entity. The base class owns everything else — the deadline, Retry-After pacing, the progress hook, the cancel, and the rule that a stream raises nothing — so stream() / astream() yield every snapshot, wait() / wait_async() are that loop with the failure policy applied, and cancel() / cancel_async() stop the wait at the next poll boundary.

A family differs in three things, all of them data or one small override: what one attempt is (_step / _step_async), which HTTP codes mean "poll again" (transient_statuses=), and what wait() hands back (_outcome). The poller returns the terminal OperationStatus; the entity mixin returns the entity.

Writing your own operation is then one method — or two, for the async twin:

from office365.runtime.operation import Operation, OperationStatus


class QueueJobOperation(Operation[str]):
    def __init__(self, queue, job_id, **options):
        super().__init__(**options)
        self._queue, self._job_id = queue, job_id

    def _step(self) -> OperationStatus:
        job = self._queue.get(self._job_id)  # one attempt
        return OperationStatus(status=job.state, percentage_complete=job.percent)

    async def _step_async(self) -> OperationStatus:
        job = await self._queue.aget(self._job_id)
        return OperationStatus(status=job.state, percentage_complete=job.percent)

    def _outcome(self, status):  # what wait() hands back
        return self._job_id


job_id = QueueJobOperation(queue, "job-7").wait(interval=2, timeout=600)

A subclass supplies _step / _step_async and, when the service has a cancel request, _send_cancel / _send_cancel_async. Two things are data rather than overrides: transient_statuses= (the HTTP codes that mean "busy, try again" rather than an outcome — 429/503 for a monitor URL, 404 too for an entity a service publishes a moment after accepting the work) and jitter= (the fraction of each delay to spread, so many clients polling one endpoint do not stay in step; 0.0 is exact). _observable(status) decides whether an attempt is worth reporting as a snapshot: a 404 gap is an attempt, not an observation.

Resume across processes: continuation tokens

The monitor URL is the only state needed to resume. ContinuationToken captures it (plus the resolution mode) as JSON, so a job can be polled in a later call, process or host:

from office365.runtime.lro import ContinuationToken, OperationPoller

# First process: submit and snapshot
poller = result.to_poller(interval=5, timeout=1800)
token = poller.to_continuation_token()
save(token.to_json())  # {"poll_url": ..., "final_state_via": "auto", "status": "inProgress"}

# Later process: resume
token = ContinuationToken.from_json(load())
poller = OperationPoller.from_continuation_token(client, token, interval=5, authenticate=True)
status = await poller.wait_async()

Graph monitor URLs are short-lived and tied to the original caller. Persist the token for the duration of the operation, not indefinitely, and re-submit the action if the service has already expired it.

Prefer: respond-async submissions

Some endpoints expose the async pattern opt-in: send Prefer: respond-async and the service may accept with 202 (or answer synchronously when it's fast). RespondAsyncRequest executes a fresh query with that preference and returns an operation either way:

  • accepted → a pollable LongRunningOperationResult carrying the monitor URL;
  • answered synchronously → a result that is already terminal, with the query's return type holding the payload. wait() returns its status without a request (status="completed", http_status = the real one, monitor_url is None).

So opting in is one line of control flow, not two:

from office365.runtime.respond_async import RespondAsyncRequest

operation = await RespondAsyncRequest(context, fresh_query, wait=5).execute_async()
status = await operation.wait_async(on_progress=lambda s: print(s.percentage_complete))
assert status.is_success

operation.is_completed says which way it went — useful for logging "answered synchronously" — but you never have to branch to get a result. This is the mechanism behind long-running workbook operations.

Batch vs long-running operations

Two different ideas, easy to confuse:

  • Batch packs many independent short requests into one HTTP round-trip. It is about how many requests travel together.
  • Long-running operations are a single request the service finishes later.

Batching an LRO does not wait for it: the 202 comes back inside the batch and you still have to poll — and the batch envelope doesn't route the monitor URL back to the operation result the way a normal query does. Keep LROs out of a batch and await them individually (shared by all of them), while batching the ordinary work:

# 1. Batch the bulk, short-lived writes.
for item in rows:
    lst.add_item(item)
await ctx.execute_batch_async(items_per_batch=100, concurrency=4)

# 2. Submit LROs on the side and await each monitor URL.
copy_result = big_file.copy(name="archive.xlsx", parent=archive).execute_query()
await copy_result.wait_for_item_async()

# Or run several LROs concurrently — each keeps the loop free.
results = [f.copy(name=f"archived-{f.name}").execute_query() for f in files]
await asyncio.gather(*(r.wait_async() for r in results))
  • Guard against batching an LRO — a small helper that detects a LongRunningOperationResult in a batch's results and fails loudly instead of silently polling nothing.

SharePoint long-running guidance

The SharePoint REST API has no single, uniform "operation status" URL the way Graph does. Depending on the workload, long-running work is finished by a service-side timer job or by the migration pipeline, and progress is either not exposed over REST or surfaced by a workload-specific endpoint.

  • When a REST-visible status endpoint exists (migration/translation jobs, tenant-level administrative operations), poll it with OperationPoller — the same pacing, timeout and continuation-token handling as Graph.
  • When it doesn't, treat the work as out-of-band: submit it, then let a durable process track completion rather than holding a request open — a SharePoint timer job, an Azure Function or a WebJob triggered on a schedule, or a queue with a visibility/Retry-After-style delay. Persist the operation id (and, where available, a continuation token) so the worker resumes after a restart.
  • Don't build fan-out on the client's lifetime. A long request that outlives your process, or a while not done: sleep() loop in a request handler, will be killed on deploy and cannot report progress. Hand it to the durable process above and surface status from there.

See Data pipeline for the bounded, resumable import/export model (checkpoint, on_error) that complements this pattern.

Examples

End-to-end scripts for the whole surface: