title: TypeError: cannot pickle 'weakref' object
body: <p>Quite new to multiprocessing here. I have a code that runs two processes. One to continuously receive data blocks from the server and put it inside a queue and the other to remove the data blocks from the queue and process it.</p>
  <p>Below is my client code:</p>
  <pre><code>
  import socket
  import turtle
  import multiprocessing
  from multiprocessing import Process, Queue
  from tkinter import *
  
  
  class GUI:
  
      def __init__(self, master):
          rec_data = recv_data()
          self.master = master
          master.title(&quot;Collision Detection&quot;)
  
          self.input_label = Label(root, text=&quot;Input all the gratings set straight wavelength values in nm&quot;)
          self.input_label.grid(row=0)
  
          self.core_string = &quot;Core &quot;
          self.entries = []
  
  
          self.label_col_inc = 0
          self.entry_col_inc = 1
          self.core_range = range(1, 5)
  
          for y in self.core_range:
              self.core_text = self.core_string + str(y) + '_' + '25'
              self.core_label = Label(root, text=self.core_text)
              self.entry = Entry(root)
              self.core_label.grid(row=1, column=self.label_col_inc, sticky=E)
              self.entry.grid(row=1, column=self.entry_col_inc)
              self.entries.append(self.entry)
              self.label_col_inc += 2
              self.entry_col_inc += 2
  
          self.threshold_label = Label(root, text=&quot;Threshold in nm&quot;)
          self.entry_threshold = Entry(root)
  
          self.threshold_label.grid(row=2, sticky=E)
          self.entry_threshold.grid(row=2, column=1)
  
          self.light_label = Label(root, text='Status')
          self.light_label.grid(row=3, column=3)
  
          self.canvas = Canvas(root, width=150, height=50)
          self.canvas.grid(row=4, column=3)
  
          # Green light
          self.green_light = turtle.RawTurtle(self.canvas)
          self.green_light.shape('circle')
          self.green_light.color('grey')
          self.green_light.penup()
          self.green_light.goto(0, 0)
  
          # Red light
          self.red_light = turtle.RawTurtle(self.canvas)
          self.red_light.shape('circle')
          self.red_light.color('grey')
          self.red_light.penup()
          self.red_light.goto(40, 0)
  
          self.data_button = Button(root, text=&quot;Get data above threshold&quot;, command=rec_data.getData)
          self.data_button.grid(row=5, column=0)
  
  
  class recv_data:
      def __init__(self):
          self.buff_data = multiprocessing.Queue()
          self.p1 = multiprocessing.Process(target=self.recvData)
          self.p2 = multiprocessing.Process(target=self.calculate_threshold)
          self.host = '127.0.0.1'
          self.port = 5001
          self.s = socket.socket()
          self.s.connect((self.host, self.port))
  
      # function to receive TCP data blocks
      def getData(self):
          len_message = self.s.recv(4)
          bytes_length = int(len_message.decode('utf-8'))  # for the self-made server
          recvd_data = self.s.recv(bytes_length)
          self.buff_data.put(recvd_data)
          self.p1.start()
          self.p2.start()
  
          self.p1.join()
          self.p2.join()
  
      def recvData(self):
          len_message = self.s.recv(4)
          while len_message:
              bytes_length = int(len_message.decode('utf-8'))  # for the self-made server
              recvd_data = self.s.recv(bytes_length)
              self.buff_data.put(recvd_data)
              len_message = self.s.recv(4)
  
          else:
              print('out of loop')
          self.s.close()
  
      def calculate_threshold(self):
          rmv_data = self.buff_data.get()
          stringdata = rmv_data.decode('utf-8')
          rep_str = stringdata.replace(&quot;,&quot;, &quot;.&quot;)
          splitstr = rep_str.split()
  
          # received wavelength values
          inc = 34
          wav_threshold = []
          for y in gui.entries:
              straight_wav = float(y.get())
              wav = float(splitstr[inc])
              wav_diff = wav - straight_wav
              if wav_diff &lt; 0:
                  wav_diff = wav_diff * (-1)
              wav_threshold.append(wav_diff)
              inc += 56
  
          threshold = float(gui.entry_threshold.get())
  
          for x in wav_threshold:
              if (x &gt; threshold):
                  gui.red_light.color('red')
                  gui.green_light.color('grey')
  
              else:
                  gui.red_light.color('grey')
                  gui.green_light.color('green')
      
  
          # function to write into the file
  
      def write_file(self, data):
          with open(&quot;Output.txt&quot;, &quot;a&quot;) as text_file:
              text_file.write('\t'.join(data[0:]))
              text_file.write('\n')
  
  if __name__ == '__main__':
      root = Tk()
      gui1 = GUI(root)
  
      root.mainloop()
  
  </code></pre>
  <p>The error I get is shown below:</p>
  <pre><code>
  Exception in Tkinter callback
  Traceback (most recent call last):
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py&quot;, line 1883, in __call__
      return self.func(*args)
    File &quot;C:/Users/PycharmProjects/GUI/GUI_multiprocess.py&quot;, line 85, in getData
      self.p2.start()
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py&quot;, line 121, in start
      self._popen = self._Popen(self)
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py&quot;, line 224, in _Popen
      return _default_context.get_context().Process._Popen(process_obj)
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py&quot;, line 326, in _Popen
      return Popen(process_obj)
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\popen_spawn_win32.py&quot;, line 93, in __init__
      reduction.dump(process_obj, to_child)
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\reduction.py&quot;, line 60, in dump
      ForkingPickler(file, protocol).dump(obj)
  TypeError: cannot pickle 'weakref' object
  Traceback (most recent call last):
    File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt;
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py&quot;, line 116, in spawn_main
      exitcode = _main(fd, parent_sentinel)
    File &quot;C:\Users\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py&quot;, line 126, in _main
      self = reduction.pickle.load(from_parent)
  EOFError: Ran out of input
  
  </code></pre>
  <p>What am I doing wrong here and how can I fix it? Any help is appreciated. Thank you!</p>