Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Engine: Add the wait argument to submit #6155

Merged
merged 2 commits into from
Oct 27, 2023

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Oct 18, 2023

For demos and tutorials, often in interactive notebooks, it is often preferred to use run instead of submit because in this way the cell will block until the process is done. The cell blocking will signal to the user that the process is still running and as soon as it returns it is immediately clear that the results are ready. With submit the cell returns immediately, but the user will now have to resort to manually checking when the process is done. Solutions are to instruct the user to call verdi process list manually (which they will have to do repeatedly) or implement some automated loop that checks for the process to terminate.

However, using run has downsides as well, most notably that the process will be lost if the notebook gets disconnected. For processes that are expected to run longer, this can be really problematic, and so submit will have to be used regardless.

Here, the wait argument is added to submit. Set to False by default to keep current behavior, when set to True, the function will mimic the behavior of run and only return when the process has terminated at which point the node is returned. A REPORT log message is emitted each time the state of the process is checked in intervals of wait_interval.

@sphuber sphuber force-pushed the feature/submit-wait branch 2 times, most recently from aec0384 to 3352c94 Compare October 23, 2023 09:36
Copy link
Member

@mbercx mbercx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sphuber! Looks good, just a few small comments and it's ready to go.

aiida/engine/launch.py Outdated Show resolved Hide resolved
docs/source/topics/processes/usage.rst Outdated Show resolved Hide resolved
docs/source/topics/processes/usage.rst Outdated Show resolved Hide resolved
docs/source/topics/processes/usage.rst Outdated Show resolved Hide resolved
For demos and tutorials, often in interactive notebooks, it is often
preferred to use `run` instead of `submit` because in this way the cell
will block until the process is done. The cell blocking will signal to
the user that the process is still running and as soon as it returns it
is immediately clear that the results are ready. With `submit` the cell
returns immediately, but the user will now have to resort to manually
checking when the process is done. Solutions are to instruct the user to
call `verdi process list` manually (which they will have to do
repeatedly) or implement some automated loop that checks for the process
to terminate.

However, using `run` has downsides as well, most notably that the
process will be lost if the notebook gets disconnected. For processes
that are expected to run longer, this can be really problematic, and so
`submit` will have to be used regardless.

Here, the `wait` argument is added to `submit`. Set to `False` by
default to keep current behavior, when set to `True`, the function will
mimic the behavior of `run` and only return when the process has
terminated at which point the node is returned. A `REPORT` log message
is emitted each time the state of the process is checked in intervals
of `wait_interval`.
@sphuber sphuber force-pushed the feature/submit-wait branch from 3352c94 to 406d103 Compare October 27, 2023 07:34
@sphuber sphuber requested a review from mbercx October 27, 2023 07:36
@sphuber sphuber force-pushed the feature/submit-wait branch from 406d103 to b6f7e2d Compare October 27, 2023 08:01
Copy link
Member

@unkcpz unkcpz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all perfect to me.
I'll let @mbercx approve it.

The recent addition of the `wait` argument to the `submit` function
allows a user to submit process the daemon, while still have the
function block until the process is terminated, as a call to `run` would
do. This can be useful in interactive tutorials and demos where the code
should not avance until the process is done, but one still wants to
benefits of having the daemon run the process.

The downside of this approach is that it only allows to submit and wait
for a single process at a time. Here the `await_processes` function is
added. It takes a list of process nodes and will wait in a loop for all
of them reach a terminal state. The time between iterations can be
controlled by the `wait_interval` argument.
mbercx
mbercx previously approved these changes Oct 27, 2023
Copy link
Member

@mbercx mbercx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one final fix needed for the Restructured text, it seems. ^^ I'll already approve, thanks again @sphuber!

One could of course also use ``run`` (see below), but then the process would be lost if the interpreter gets accidentally shut down.
By using ``submit``, the process is run by the daemon which takes care of saving checkpoints so it can always be restarted in case of problems.
If you need to launch multiple processes in parallel and want to wait for all of them to be finished, simply use ``submit`` with the default ``wait=False`` and collect the returned nodes in a list.
You can then pass them to :func:`aiida.engine.launch.await_processes` which will return once all processes have terminated::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can then pass them to :func:`aiida.engine.launch.await_processes` which will return once all processes have terminated::
You can then pass them to :func:`aiida.engine.launch.await_processes` which will return once all processes have terminated:

I think the extra colon is still causing trouble here.

Screenshot 2023-10-27 at 17 35 40

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that. I tested the docs locally and it passed, but didn't bother to look at the actual output. Funnily enough, with just a single colon VSCode actually has incorrect syntax highlighting and lights up the whole phrase starting from :func: onwards. This was fixed by the double colon, but clearly that is not what restructured text wants.

I also noted that I forgot to update sleep_interval to wait_interval which I corrected now

@sphuber sphuber force-pushed the feature/submit-wait branch from b6f7e2d to f91aa09 Compare October 27, 2023 15:52
@sphuber sphuber merged commit 45767f0 into aiidateam:main Oct 27, 2023
18 checks passed
@sphuber sphuber deleted the feature/submit-wait branch October 27, 2023 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants