Async Execution
Use asynchronous=True when you call a supported jcmsuite operation, such as
jcmsuite.solve, from a running event loop. An event loop runs
asynchronous operations. The operation returns a coroutine, a value that the event loop
can pause and resume. Use await to get the operation result.
Use synchronous calls in a script that does not need concurrent work. Use asyncio.run(...)
when a script must call an asynchronous operation. Use await directly in a notebook or
service that already has an event loop.
Run From A Script
In a script, define one async entry point and start it at the top level:
import asyncio
import jcmsuite
async def main() -> None:
result = await jcmsuite.solve(
"project.jcmp",
asynchronous=True,
)
print(result.project_file)
asyncio.run(main())
Run Inside An Event Loop
If a notebook or service already has an event loop, set asynchronous=True and await the
result. Run the following code inside that existing event loop:
import jcmsuite
result = await jcmsuite.solve(
"project.jcmp",
asynchronous=True,
)
print(result.project_file)
Do not call jcmsuite.solve without asynchronous=True from a
running event loop.
The function raises RuntimeError under this condition.
If the operation raises an exception, handle the exception in the coroutine or in the caller that awaits the coroutine. Do not start a second event loop from an already running event loop.
Run Concurrent Solves
Create one coroutine for each independent solve. Use
asyncio.gather(...)
to await the coroutines together. The following example runs within a running event loop:
import asyncio
import jcmsuite
solve_a = jcmsuite.solve(
"project.jcmp",
keys={"radius": 0.3},
temporary=True,
asynchronous=True,
)
solve_b = jcmsuite.solve(
"project.jcmp",
keys={"radius": 0.5},
temporary=True,
asynchronous=True,
)
result_a, result_b = await asyncio.gather(solve_a, solve_b)
If concurrent solves write files, give each solve a separate working_dir or set
temporary=True. Do not let concurrent solves write to the same working directory.
Use Daemon Submission
Daemon submission and collection also support asynchronous=True. The following example
runs within a running event loop:
import jcmsuite
with jcmsuite.daemon.handler() as daemon:
submission = await jcmsuite.solve(
"project.jcmp",
dispatch="submit",
asynchronous=True,
)
result = await submission.wait(asynchronous=True)
You can also call
daemon.wait(submission, asynchronous=True).
Configure the daemon resources before you submit work. See Daemon Execution and
Submit and Wait.
See the Use Async Execution tutorial for a runnable batched and concurrent solve example.