How to properly close app in pysiril?

Hi all,

For me closing app doesn’t work and I have to kill/close powershell each time.
What I am doing wrong?

Simplified script do demo my issue:

import os
from pysiril.siril   import *
from pysiril.wrapper import *

try:

  startdir=os.getcwd()

  app=Siril()
  cmd=Wrapper(app)
  app.Open()
  try:
          cmd.set32bits()
  except Exception as e:
          pass


  os.chdir(startdir)
  app.Close()
  del app
  
except Exception as e:
  os.chdir(startdir)
  app.Close()
  del app
  raise

Output of the script:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\Users\virog> python P:\test.py
WARNING: pysiril uses by default :C:/Program Files/SiriL/bin/siril.exe
INFO   : VERSION siril 1.0.2 :
INFO   : Siril is compatible with pysiril
INFO   : Initialisation pySiril V0.0.12 : OK
INFO   : First step: Starting 'Siril -p' ....
INFO   : ThreadSiril is started
INFO   : run : C:/Program Files/SiriL/bin/siril.exe -p
INFO   : waiting:
7s
INFO   : Second step: Starting pipe reader ....
INFO   : ThreadReader is started
INFO   : Third step: Starting pipe writer ....
INFO   : PipeWriter is started
INFO   : Open()
> set32bits
: log: Running command: set32bits
: log: 32-bit per channel in processed images mode is active
> [status: success]
INFO   : Stopping ThreadReader ...
INFO   : Stopping PipeWriter ...
INFO   : ThreadReader is aborted
INFO   : ThreadReader is stopped
INFO   : PipeWriter is stopped
INFO   : Close()
Stopping Trace ...
Trace is stopped

Hi,

don’t know exactly what’s happening on your end… just copied your code and fixed the indentations which may have been messed up by pasting on the forum. Just ran through powershell, it exits correctly at the end, I get powershell back.
Can you check in the task manager if you still have siril processes hanging?

C.

Thank you!

It looks like problem gone for me too without any intervention from my side.

Sorry for returning back with the same, but for me it looks pysiril with Siril can stuck in some kind of condition when exit() from scripts doesn’t work.

Problem that I am calling some scripts from N.I.N.A sequences and scrips block sequences until kill a script.

Usually this sticks triggered by some kind of errors in script. I do understand I have do debug my scripts better, but I am not professional programmer, scripts are big and depends on many external variables, so I still have issues with app.Close()

Simple way to reproduce the issue but running this script (wrong indent for first cmd.help()):

from pysiril.siril import Siril
from pysiril.wrapper import Wrapper
app=Siril()
cmd=Wrapper(app)
app.Open()
 cmd.help()
cmd.help()
app.Close()
del app
exit()

Problem that after that even I will kill all Siril and python process pysiril will not exit:

C:\Users\virog>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pysiril.siril import Siril
>>> from pysiril.wrapper import Wrapper
>>> app=Siril()
WARNING: pysiril uses by default :C:/Program Files/SiriL/bin/siril.exe
INFO   : VERSION siril 1.0.3 :
INFO   : Siril is compatible with pysiril
INFO   : Initialisation pySiril V0.0.12 : OK
>>> cmd=Wrapper(app)
>>> app.Open()
INFO   : First step: Starting 'Siril -p' ....
INFO   : ThreadSiril is started
INFO   : run : C:/Program Files/SiriL/bin/siril.exe -p
INFO   : waiting:
7s
INFO   : Second step: Starting pipe reader ....
INFO   : ThreadReader is started
INFO   : Third step: Starting pipe writer ....
INFO   : PipeWriter is started
INFO   : Open()
True
>>> cmd.help()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\virog\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pysiril\wrapper.py", line 694, in help
    str=help.search(name)
  File "C:\Users\virog\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pysiril\lib\help.py", line 1304, in search
    strhelp = _("commands of pysiril/siril :\n")
TypeError: 'bool' object is not callable
>>> app.Close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\virog\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pysiril\siril.py", line 251, in Close
    self.thReader.Stop()
  File "C:\Users\virog\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pysiril\lib\PipeReader.py", line 102, in Stop
    self.tr.info( _("Stopping") +" ThreadReader ...")
TypeError: 'bool' object is not callable
>>> del app
>>> exit()

Could you please take a look what can be done to close script in this like cases?

Without app.open() it works:

from pysiril.siril import Siril
from pysiril.wrapper import Wrapper
app=Siril()
cmd=Wrapper(app)
cmd.help()
app.Close()
del app
exit()


NOP. Sorry, It doesn’t work at all even if app was closed immediately after it was opened.
No any python or Siril processes in task manager:

C:\Users\virog>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pysiril.siril import Siril
>>> from pysiril.wrapper import Wrapper
>>> app=Siril()
WARNING: pysiril uses by default :C:/Program Files/SiriL/bin/siril.exe
INFO   : VERSION siril 1.0.3 :
INFO   : Siril is compatible with pysiril
INFO   : Initialisation pySiril V0.0.12 : OK
>>> cmd=Wrapper(app)
>>> app.Open()
INFO   : First step: Starting 'Siril -p' ....
INFO   : ThreadSiril is started
INFO   : run : C:/Program Files/SiriL/bin/siril.exe -p
INFO   : waiting:
7s
INFO   : Second step: Starting pipe reader ....
INFO   : ThreadReader is started
INFO   : Third step: Starting pipe writer ....
INFO   : PipeWriter is started
INFO   : Open()
True
>>> app.Close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\virog\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pysiril\siril.py", line 251, in Close
    self.thReader.Stop()
  File "C:\Users\virog\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pysiril\lib\PipeReader.py", line 102, in Stop
    self.tr.info( _("Stopping") +" ThreadReader ...")
TypeError: 'bool' object is not callable
>>> del app
>>> exit()

Rebooted. Nothing started.

Microsoft Windows [Version 10.0.19044.1826]
(c) Microsoft Corporation. All rights reserved.

C:\Users\virog>python
Python 3.10.5 (tags/v3.10.5:f377153, Jun  6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pysiril.siril import Siril
>>> app=Siril()
WARNING: pysiril uses by default :C:/Program Files/SiriL/bin/siril.exe
INFO   : VERSION siril 1.0.3 :
INFO   : Siril is compatible with pysiril
INFO   : Initialisation pySiril V0.0.12 : OK
>>> app.Open()
INFO   : First step: Starting 'Siril -p' ....
INFO   : ThreadSiril is started
INFO   : run : C:/Program Files/SiriL/bin/siril.exe -p
INFO   : waiting:
7s
INFO   : Second step: Starting pipe reader ....
INFO   : ThreadReader is started
INFO   : Third step: Starting pipe writer ....
ERROR  : broken pipe (r), bye bye
INFO   : PipeWriter is started
ERROR  : *** cpipe::open() (2, 'CreateFile', 'The system cannot find the file specified.')
ERROR  : broken pipe (w), bye bye
ERROR  : ThreadReader is aborted
ERROR  : *** cpipe::open() (2, 'CreateFile', 'The system cannot find the file specified.')
ERROR  : PipeWriter is aborted
False
>>> app.Close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\virog\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pysiril\siril.py", line 248, in Close
    self.tr.warning(_("pysiril is already closed"))
TypeError: 'bool' object is not callable
>>> del app
>>> exit()




Still fighting with pysiril on my mini-PC which continue to reject cooperate.

Using taskkill after app.Close() and it looks it works:

  app.Close()
  del app
  log(INFO,"!!!!!!!!!! Creating master flats finished !!!!!!!!!!")
  os.system("taskkill /IM siril.exe /F")
  
except Exception as e:
  os.chdir(startdir)
  if not delete_tmpfiles_from_procdir(conf["procdir"]):
    log(ERROR,"Failed to remove temporary files from processing directory {}".format(conf["procdir"]))
  log(ERROR,"Creating master flats error - {}".format(e))
  app.Close()
  del app
  os.system("taskkill /IM siril.exe /F")
  raise
  exit(1)

Trying to check if app.Open successful:

  if not app.Open():
    log(ERROR,"Can not start Siril. Script aborted")
    os.system("taskkill /IM siril.exe /F")
    time.sleep(60)
    exit(1)

With some errors it works:

INFO   : First step: Starting 'Siril -p' ....
INFO   : ThreadSiril is started
INFO   : run : C:/Program Files/SiriL/bin/siril.exe -p
INFO   : waiting:
7s
INFO   : Second step: Starting pipe reader ....
INFO   : ThreadReader is started
INFO   : Third step: Starting pipe writer ....
INFO   : PipeWriter is started
ERROR  : broken pipe (r), bye bye
ERROR  : broken pipe (w), bye bye
ERROR  : *** cpipe::open() (2, 'CreateFile', 'The system cannot find the file specified.')
ERROR  : *** cpipe::open() (2, 'CreateFile', 'The system cannot find the file specified.')
ERROR  : ThreadReader is aborted
ERROR  : PipeWriter is aborted
ERROR  : Can not start Siril. Script aborted
SUCCESS: The process "siril.exe" with PID 8172 has been terminated.
INFO   : ThreadSiril is stopped
INFO   : Closing logfile

With some errors it looks it doesn’t return False:

INFO   : First step: Starting 'Siril -p' ....
INFO   : ThreadSiril is started
INFO   : run : C:/Program Files/SiriL/bin/siril.exe -p
INFO   : waiting:
7s
INFO   : Second step: Starting pipe reader ....
INFO   : ThreadReader is started
INFO   : Third step: Starting pipe writer ....
INFO   : PipeWriter is started
ERROR  : broken pipe (r), bye bye
INFO   : Open()
ERROR  : *** cpipe::open() (2, 'CreateFile', 'The system cannot find the file specified.')
ERROR  : ThreadReader is aborted
> set32bits
> [abort]
> cd 'c:/imaging/processing'
> [abort]
> preprocess FLAT_ANGL0_L -bias==256
> [abort]
> stack pp_FLAT_ANGL0_L_ rej  3 3 -norm=mul -out=c:/Siril/flats/pp_FLAT_ANGL0_L_stacked.fits
> [abort]
> close
> [abort]
> load c:/Siril/flats/pp_FLAT_ANGL0_L_stacked.fits
> [abort]
> stat
> [abort]
ERROR  : Stacking FLAT_ANGL0_L failed: cannot unpack non-iterable bool object
ERROR  : Failed to remove temporary files from processing directory c:/imaging/processing
INFO   : Stopping PipeWriter ...
INFO   : PipeWriter is stopped
INFO   : Close()
Stopping Trace ...
Trace is stopped

Windows 10 and always updated.
What can be wrong?