micro-manager/pycro-manager

Script hangs in headless mode

jacopoabramo opened this issue · 8 comments

Greetings,

I tried running the following snippet from command line:

from pycromanager import Core, start_headless
from pymmcore_plus import find_micromanager 

import logging

logging.basicConfig(level=logging.INFO)

logger = logging.getLogger()

mm_app_path = find_micromanager()

logger.info("Found Micro-Manager at: " + mm_app_path)

config_file = mm_app_path + "/MMConfig_demo.cfg"

# Start the Java process
start_headless(mm_app_path, config_file)

logger.info("Started Micro-Manager headless, creating core... ")

core = Core()

logger.info("Done")

The script executes fine but after the last logging line the CLI hangs and I have to manually insert a keyboard interrupt to make the script stop. Am I doing something incorrectly?

Python version: 3.8.10
Pycromanager version: 0.28.1
MM Studio version: 2.0.2 20230819 (nightly build)
MMCore version: 10.4.0
Device API version 71, Module API version 10

Hi Jacapo, try:

from pycromanager.acq_util import cleanup
cleanup()

At the end to shut down the headless process.

Probably this needs to be added to the documentation (#673)

Hi @henrypinkard , thanks for replying. I tried it, it still hangs after calling cleanup

You're right. That function wasn't working. I just fixed it #674

I also changed the name to stop_headless

@jacopoabramo
I've just made some fixes to this in the latest versions (pypi/nightly build), let me know if you run into any more problems

Hi @henrypinkard , sorry for the late reply. I tested the new changes with the same code snippet, but even after calling stop_headless it still hangs. Furthermore, when enabling the debug flag on the start_headless function, this prints empty lines. The stop_headless function instead - with debug=True - correctly prints out debug information but the command line still hangs

Oh it appears this is because core doesn't get garbage collected. I just was able to get this to work properly by adding in core = None. This is a bit confusing...I'm going to see if there's a way for this to happen automatically:

this script works for me:

config_file = mm_app_path + "/MMConfig_demo.cfg"

print('starting headless')
start_headless(mm_app_path, config_file)
print('creating core')
core = Core()
core = None
stop_headless()
print('ending')

Okay I think this is actually fixed now by #700. You shouldn't need to set core = None. Let me know if this works for you

Hi @henrypinkard , that did the trick. Now the test script works either with or without the stop_headless method. Closing the issue.