Running a python module in the background

Hi everyone,

I’m using the latest 3D Slicer version (4.13.20201207) and I have been working on a python scripted module. The module requires two inputs (by the user) and there is a button to run the algorithm. I would like to run this module in the background but I don’t know how to start doing it (I never did it before).

Can you advise me on how to do that? I am not familiar with C++.

Thank you in advance.

You can create a new Python scripted CLI (command-line interface) module, which performs the time-consuming processing in the background in a separate process. Your interactive Python scripted loadable module remains almost the same, it just starts the CLI module and adds an observer (or checks status of the CLI module node from time to time). You can find an example of a Python scripted CLI module here and how to run CLI module in the background here.

Thank you so much for replying.
I will look into your suggestions and if I have questions I will come back again.

1 Like

Hi again,

I am trying to run a complex algorithm in the background but I am getting some issues.
The main part of the algorithm is to do image registration and I am using the Elastix registration available in Slicer. I would like to run it in the background, but since it is a scripted loaded module how can I do it?
I also needed access to python packages and class objects from other python modules, can I do it using the cli module?
Is it possible to use a class object as parameter of cli module?

Sorry for the questions. I have a scripted python module 100% working but I would like to run it in the background in order to use Slicer at the same time.

Thank you for the help.

Elastix is already a separate process, this is even easier, because you don’t need to create a CLI. All you need to change is to not block the application while Elastix is running. Instead of blocking the application by using this while loop check the status of Elastix execution in a function that is called regularly by a QTimer. You can make this change in your SlicerElastix module or you can copy relevant parts from SlicerElastix module to your module.

Since only Elastix process will run in the background, everything else can remain in a regular Python scripted module. No need for Python CLI module for your case.

Hello again,

I managed to run Elastix without freeze Slicer, but not the rest of the code. Anyway, I would like to know if it is possible (and how) to perform the following tasks automatically:

1- Connect Slicer into a server. Whenever new DICOM data is on the server, import it into the Slicer’s database.

2- Run a python scripted module (in the Slicer’s module list) with the new data.

I would appreciate any suggestion. P.S: I’m no a programming expert.

There is no DICOM service that allows an application entity to “subscribe” to all new images. You can set up auto-push on the server, so that the server sends any new images to the Slicer listening server (C-STORE SCP). You could also poll the server (Slicer acts as C-FIND SCU) and retrieve (Slicer acts as C-MOVE SCU) if something new is found, which is more complicated, but may be necessary if auto-push is not available.

Received DICOM images are automatically added to the database. To automatically process or incoming data, you can connect functions to patientAdded, studyAdded, etc. signals emitted by the ctkDICOMDatabase.