1

I am creating a small GUI where user clicks a push button so the numerical simulation code in FORTRAN starts and its execution is shown with busy indicator movement (front and back).

With pool.apply_async method I could able to run the FORTRAN code and busy indicator successfully. But I have a problem when I stop the process and restart it. When I restart the process, it is not running and throwing an assertion error. (assert self._state == RUN)

Note: I am not supposed to disturb the FORTRAN code at any moment. Because it is a fixed one written for some numerical simulation. Its execution takes more than 30 mins

Below is my code:

import sys
from PyQt4 import QtGui  
from PyQt4 import QtCore
from PyQt4 import uic

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import FortranSimulation
import time

qtCreatorFile = "you_atmpt.ui"                # Loading UI file
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)


def pass_args(args):
   b = 10.0
   print b
   b = b + 2
   FortranSimulation.fortrancode(b)
   return b

class MainClass(QtGui.QMainWindow,Ui_MainWindow):    # MAin GUI class
   def __init__(self):
   super(MainClass,self).__init__()
   self.setupUi(self)                         # 2 PB
   self.StartCalculaltion_button.clicked.connect(self.Fortran_Calc)   
   self.Stop_button.clicked.connect(self.Stop_All)
   self.pool = mp.Pool(None)


def Fortran_Calc(self):# Initiation of other process for 'Fortran Calculation'
   self.label_2.setText('Calculation is in progress!')
   self.progressBar.setRange(0,0)
   index = 1
   surface_id = 2
   self.pool.apply_async(pass_args, [(surface_id, index)], 
                              callback=self.callback)  

def callback(self,b):
    print b
    self.label.setText('Calculation is completed!')

def Stop_All(self):              # Termination of process
    self.progressBar.setRange(0,1)
    self.pool.close()
    self.pool.terminate()


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainClass()
    window.show()
    app.exec_()

I added the error message also: enter image description here

I tried with self.pool.close(), self.pool.terminate() these commands are stopping the process. At the same time I can't restart the process again with the start button.

And also I would like to know whether restarting a process is possible or not?

Thanks in advance.

krish
  • 41
  • 1
  • 7
  • Probably the fortran code doesn't release the [GIL](https://stackoverflow.com/questions/1294382/what-is-a-global-interpreter-lock-gil). Use [multiprocessing](https://docs.python.org/3/library/multiprocessing.html#module-multiprocessing) instead. – ekhumoro Dec 03 '17 at 16:44
  • I will check it and see, how it works. Thank you! – krish Dec 03 '17 at 19:42
  • @ekhumoro I implemented your suggestion, it works fine but there is small problem. Please read the above description. I have mentioned my problem in the code. Can you please suggest me what I should do for making my GUI process to know that signal has been received? Thank you! – krish Dec 08 '17 at 11:40
  • 1
    With multiprocessing, you should use a callback to post custom event, rather than emit a signal, See [this answer](https://stackoverflow.com/a/47490096/984421) for a working example. – ekhumoro Dec 08 '17 at 17:27
  • @ekhumoro Thank you so much for all the time you spent on this. I am very beginner to python and pyqt4. I am not much familiar with the concepts. I will keep my question very simple. please read my description and can you give me some hints now. – krish Dec 09 '17 at 16:02
  • It doesn't make any sense to use a progress indicator, since you have no idea how long the fortran code will take to complete, and it does not provide incremental feedback. I don't understand what you're trying to do with the thread or the signals. I think the best you can do is show a busy indicator whilst the calculation is running, and then provide a notification when it completes. For the busy indicator, set the progress bar range to `0,0`; for the other part, see the link in my previous comment. – ekhumoro Dec 09 '17 at 17:32
  • I will work on it and let you know. Thank you. – krish Dec 09 '17 at 18:33
  • @ekhumoro I was not known about busy indicator, So I was trying it in a different way. Now I could implement busy indicator. And using QThread I tried executing FORTRAN code separately. Currently I face a weird small problem. please read my description and can you give me some hints now. – krish Dec 10 '17 at 23:27
  • See my first comment above. Not sure why you're persisting with multi-threading, when you already know it doesn't work. If you adapted the code in the answer I linked to previously, I think you'd have this working very quickly. – ekhumoro Dec 11 '17 at 00:29
  • @ekhumoro Thank you so much I could able to implement callback function successfully. But I have small error with pause & Restart of pool.apply_async method. please read my description and can you give me some hints now. – krish Dec 11 '17 at 18:47
  • 1
    Try creating a new `Pool` when starting the calculation, and call `pool.terminate()` when it's finished, or when you stop it. – ekhumoro Dec 11 '17 at 20:53

0 Answers0