@@ -78,3 +78,71 @@ def generate_structures(element: str, factors: list) -> dict:
7878print ("Generated scaled structures:" )
7979for key , value in result .items ():
8080 print (key , value )
81+
82+
83+ # %%
84+ # Async functions
85+ # ---------------
86+ # ``pyfunction`` also supports Python's ``async`` functions. This is a powerful feature for
87+ # tasks that are I/O-bound (e.g., waiting for network requests, file operations) or for
88+ # running multiple tasks concurrently without blocking the AiiDA daemon.
89+ #
90+ # When you ``submit`` an async function, the call returns immediately with a process node,
91+ # allowing your script to continue running while the function executes in the background.
92+ #
93+
94+ import asyncio
95+ from aiida .engine import run , submit
96+ import datetime
97+ from aiida_pythonjob import prepare_pyfunction_inputs
98+
99+
100+ @pyfunction ()
101+ async def add_async (x , y , time : float ):
102+ """A simple function that adds two numbers."""
103+ import asyncio
104+
105+ # Simulate asynchronous I/O or computation
106+ await asyncio .sleep (time )
107+ return x + y
108+
109+
110+ inputs = prepare_pyfunction_inputs (
111+ add_async ,
112+ function_inputs = {"x" : 2 , "y" : 3 , "time" : 2.0 },
113+ )
114+
115+ node = submit (add_async , ** inputs )
116+
117+ # %%
118+ # Here is an example to monitor external events or conditions without blocking.
119+ # Here is an example that waits until a specified time.
120+ #
121+
122+
123+ @pyfunction ()
124+ async def monitor_time (time : datetime .datetime ):
125+ # monitor until the specified time
126+ while datetime .datetime .now () < time :
127+ print ("Waiting..." )
128+ await asyncio .sleep (0.5 )
129+
130+
131+ inputs = prepare_pyfunction_inputs (
132+ monitor_time ,
133+ function_inputs = {"time" : datetime .datetime .now () + datetime .timedelta (seconds = 5 )},
134+ )
135+
136+ node = submit (monitor_time , ** inputs )
137+
138+ # %%#
139+ # Killing an async process
140+ # ------------------------
141+ # Since async functions run as regular AiiDA processes, they can be controlled and killed
142+ # programmatically. This is useful for managing long-running or stuck tasks.
143+ # You can kill a running async function using the AiiDA command line interface.
144+ #
145+ # .. code-block:: bash
146+ #
147+ # $ verdi process kill <pk>
148+ #
0 commit comments