Restarting process raises runtimeerror (can't create new thread at interpreter shutdown)

46 Views Asked by At

I have two processes: P1 and P2. P1 send data to P2 (with shared memory).

When something goes wrong to P2 a PyQt5 Window with the error message is opened. One option in the window is to restart process P2 by terminated and re-instantiating.

After this operation the P1 process resend data to P2.

When this happens this error oquers:

Traceback (most recent call last):
  File "C:\Users\chris\Documents\My Projects\papinhio-player\src\python+\main-window\../..\python+\main-window\final-slice.py", line 635, in final_slice_ready
    self.main_self.final_slice_plot_instance.final_slice_plot_queue.put({"type":"slice","slice":slice})
  File "C:\Python\Lib\multiprocessing\queues.py", line 94, in put
    self._start_thread()
  File "C:\Python\Lib\multiprocessing\queues.py", line 192, in _start_thread
    self._thread.start()
  File "C:\Python\Lib\threading.py", line 992, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't create new thread at interpreter shutdown

Why this is happened and how can i resolve it?

from multiprocessing import Process, Queue, Pipe
import time

class Proc_1(Process):
    def __init__(self,q_1):
        super().__init__()
        self.daemon = False
        self.q_1 = q_1
        
    def run(self):
        while(True):
            time.sleep(0.125)
            self.q_1.put("data...")
            
class Proc_2(Process):

    def __init__(self,q_1):
        super().__init__()
        self.daemon = False
        self.q_1 = q_1
        
    def run(self):
        while(True):
            data = self.q_1.get()
            print(data)
            
if __name__ == "__main__":
    q_1 = Queue()
    proc_1 = Proc_1(q_1)
    proc_1.start()
    proc_2 = Proc_2(q_1)
    proc_2.start()
    time.sleep(1)
    proc_2.terminate()
    q_1 = Queue()
    proc_2 = Proc_2(q_1)
    proc_2.start()

The above simple example works.

Here is the error snippet:

import time
from PyQt5.QtCore import pyqtSignal, QThread
from multiprocessing import Process, Queue, Pipe
from datetime import datetime, timedelta
import traceback
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.dates import num2date
from matplotlib.ticker import FuncFormatter
import numpy as np


class Final_Slice_Plot:

    def __init__(self, main_self):
        try:
            self.main_self = main_self

            # chart
            self.chart = Canvas(self)
            self.chart.ax.set_facecolor((1, 1, 1))
            self.chart.ax.tick_params(labelcolor='white')

            # create process
            self.process_number = 94
            self.final_slice_plot_mother_pipe, self.final_slice_plot_child_pipe = Pipe()
            self.final_slice_plot_queue = Queue()
            self.final_slice_plot_queue.put({"type":"test"})
            print("test")
            self.final_slice_plot_emitter = Final_Slice_Plot_Emitter(self.final_slice_plot_mother_pipe)
            self.final_slice_plot_emitter.error_signal.connect(lambda error_message: self.main_self.open_final_slice_plot_error_window(error_message))
            self.final_slice_plot_emitter.plot_data_signal.connect(lambda x,y: self.plot(x,y))

            self.final_slice_plot_emitter.start()
            self.final_slice_plot_child_process = Final_Slice_Plot_Child_Proc(self.final_slice_plot_child_pipe, self.final_slice_plot_queue)
            self.final_slice_plot_child_process.start()

            counter = 0
            for process in self.main_self.manage_processes_instance.processes:
                if "process_number" in process:
                    if process["process_number"] == self.process_number:
                        self.main_self.manage_processes_instance.processes[counter][
                            "pid"] = self.final_slice_plot_child_process.pid
                        self.main_self.manage_processes_instance.processes[counter]["start_datetime"] = datetime.now()
                        self.main_self.manage_processes_instance.processes[counter]["status"] = "in_progress"
                counter += 1

            if self.main_self.manage_proccesses_window_is_open:
                self.main_self.manage_proccesses_window_support_code.manage_proccesses_queue.put(
                    {"type": "table-update", "processes": self.main_self.manage_processes_instance.processes})
            self.main_self.final_slice_instance.put_to_plot = True
        except:
            error_message = traceback.format_exc()
            print("ERROR")
            self.main_self.open_final_slice_plot_error_window(error_message)

In the first run "test" message in printed in console but after restart this error appeared in the error window:

Traceback (most recent call last):
  File "C:\Users\chris\Documents\My Projects\papinhio-player\src\python+\main-window\../..\python+\main-window\final-slice-plot.py", line 28, in __init__
    self.final_slice_plot_queue.put({"type":"test"})
  File "C:\Python\Lib\multiprocessing\queues.py", line 94, in put
    self._start_thread()
  File "C:\Python\Lib\multiprocessing\queues.py", line 192, in _start_thread
    self._thread.start()
  File "C:\Python\Lib\threading.py", line 992, in start
    _start_new_thread(self._bootstrap, ())
RuntimeError: can't create new thread at interpreter shutdown
0

There are 0 best solutions below