bluesky / bluesky-queueserver

Server for queueing plans
https://blueskyproject.io/bluesky-queueserver/
BSD 3-Clause "New" or "Revised" License
12 stars 22 forks source link

Option to start IPython kernel in the worker process #267

Closed dmgav closed 1 year ago

dmgav commented 1 year ago

This PR is a significant extension of the Queue Server functionality. There are no significant changes to the way how Queue Server operates in normal mode. All the existing API are expected to function identically in the normal 'Python' and in the extended 'IPython' modes. There are no meaningful changes to the API. (re_pause API requests are now accepted by the server whenever Run Engine is in the running state, independently on the states of the manager or the worker).

The new features are still in the development stage. Unit tests for IPython-specific features and updates to documentation will be implemented in the following PRs.

Description

If start-re-manager is started with --use-ipython-kernel=ON parameter, opening the environment starts an IPython kernel in the worker process. The startup code is loaded during kernel initialization from the specified profile (--ipython-dir and --startup-profile parameters). Optionally the code may be loaded from a module (--startup-module) or a script (--startup-script), but the startup code from the specified profile (or default IPython profile) is always loaded. The startup code is not patched and loaded as is. The code may contain IPython-specific elements, like magics. Matplotlib backend may be passed using --ipython-matplotlib parameter (accepts same values as --matplotlib parameter of IPython, e.g. --ipython-matplotlib=qt5. If run on a beamline workstation, the IPython-based worker is expected to support standard live plotting designed for IPython workflow.

Once the environment is open and the IPython kernel is running, it can be connected with Jupyter console. On the same workstation the kernel may be accessed as jupyter console --existing. It should be possible to access the kernel from a different workstation by feeding it the connection information (it is expected that API to do it conveniently). Note, that currently the 0MQ ports used by the kernel are temporarily fixed, but it is expected that they will be randomly generated in the future (this is default behavior of IPython kernel that we want to replicate). The kernel is single-threaded, i.e. the manager and the console are blocking each other. Jupyter console is going to be unresponsive when the manager is running a plan, and the manager can not start a plan when there is a task started from console. Closing the Jupyter console does not stop the task, so a plan started from the console will continue running and the output will be displayed in the manager output. It is possible to start a plan in the manager, pause it, then resume from the console. If a plan started from the manager is resumed and completed from the console, the respective entry is added to history (no traceback is going to be added if the plan failed). Plans started from the console can not currently be resumed from the manager.

Note: Plans running in the console can not be paused using Ctrl-C (it just temporarily disrupts output to the console). Use qserver re pause to pause the plan.

Motivation and Context

Summary of Changes for Release Notes

Added

Changed

How Has This Been Tested?

While unit tests for some of the new features were implemented, there are no tests for IPython specific behavior. Developing IPython-specific tests requires significant work and will be a subject of next PRs. RE Manager with IPython-based worker passes all the existing tests (~1800), which is the reason to assume it is fully compatible with the original Python kernel. The IPython specific behavior was tested manually on with simulated plans and devices and on SRX and HXN beamlines at NSLS-II.