title: Multiprocessing connection between two processes at the same time with TCP
body: <p>I'm kind of a rookie in python coding and I am trying to learn multiprocessing for a problem that I have.</p>
  
  <p>I have a project with a raspberry pi(linux) and from another PC/device(windows) I am trying to share data between them with TCP connection. I made the server/client code that is working (server on raspberry and client on PC).</p>
  
  <p>Next step is to make a visual application (in this case I choose kivy for that) and directly connect and send data from my PC to my raspberry by this application, PC being the client here directly from python/kivy code.</p>
  
  <p>The problems are, when I connect introducing the IP and PORT in my kivy application the script is connecting me, but immediately kill the server and I want my server to be continuously alive all the time.</p>
  
  <p>Other problem, imagine the server is not killed by the code, in the other function named send_data I want to send the speed and angle variable to my server and still be connected to the server and the server be alive, SO my only option was multiprocessing. </p>
  
  <p>Code for server on raspberry:</p>
  
  <pre><code>import socket
  
  host = ''
  port = int(input("Port: "))
  
  speed = []
  angle = []
  
  def setupServer():
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     print ("Socket created")
     try:
        s.bind((host, port))
     except socket.error as msg:
        print(msg)
     print("Socket bind complete.")
     return s
  
  def setupConnection():
     s.listen(1)
     conn, address = s.accept()
     print("Connected to: " +address[0] + ":" + str(address[1]))
     return conn
  
  def dataTransfer(conn):
     global speed,angle
  
     data = conn.recv(1024)
     data = data.decode('utf-8')
     """ split the data """
     dataMessage = data.split(' ',1)
     speed = dataMessage[0]
     angle = dataMessage[1]
  
     print(speed)
     print(angle)
  
     if dataMessage[0] == 'exit':
         print("Client has left the server")
         break
     if dataMessage[0] == 'kill':
         print("Server is shutting down")
         s.close()
         break
  
     conn.close()
  
  
  s = setupServer()
  
  while True:
     try:
        conn = setupConnection()
        dataTransfer(conn)
     except:
        break
  </code></pre>
  
  <p>Code for my client with kivy module:</p>
  
  <pre><code>import kivy
  import socket
  import multiprocessing
  
  kivy.require('1.10.1')
  
  from kivy.app import App
  from kivy.uix.button import Label
  from kivy.uix.gridlayout import GridLayout
  from kivy.uix.textinput import TextInput
  from kivy.uix.button import Button
  from kivy.config import Config
  from kivy.uix.widget import Widget
  from kivy.properties import ObjectProperty
  
  
  Config.set('graphics', 'width', '400')
  Config.set('graphics', 'height', '400')
  
  
  class MyGrid(Widget):
      ip = ObjectProperty(None)
      port = ObjectProperty(None)
      speed = ObjectProperty(None)
      angle = ObjectProperty(None)
  
      def connecting(self):
          h = self.ip.text
          p = int(self.port.text)
  
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          s.connect((h, p))
          self.lbl.text = "Connected to server!"
  
          return s
  
      def __init__(self):
          super(MyGrid, self).__init__()
  
      def send_data(self):
          h = self.ip.text
          p = int(self.port.text)
          speed = self.speed.text
          angle = self.angle.text
  
          s = MyGrid.connecting(h, p)
  
          var = speed + " " + angle
          s.send(str.encode(str(var)))
  
          #reply = s.recv(1024)
          #print(reply.decode('utf-8'))
          self.lbl.text = "Speed and angle has been send!"
  
          s.close()
  
      def dct(self):
          h = self.ip.text
          p = int(self.port.text)
  
          s = MyGrid.connecting(h, p)
          s.close()
          self.lbl.text = "Client disconnected!"
  
      def stop_srvv(self):
          h = self.ip.text
          p = int(self.port.text)
  
          s = MyGrid.connecting(h, p)
  
          s.send(str.encode(str("kill ")))
  
          reply = s.recv(1024)
          print(reply.decode('utf-8'))
          self.lbl.text = "Server stopped!"
  
  
  class MyApp(App):
      def build(self):
          return MyGrid()
  
  
  if __name__ == "__main__":
      MyApp().run()
  </code></pre>
  
  <p>Code for kivy styling part:</p>
  
  <pre><code>&lt;MyGrid&gt;
      lbl: my_label
      ip: ip
      port: port
      speed: speed
      angle: angle
  
      GridLayout:
          cols: 1
          size: root.width, root.height
  
          GridLayout:
              cols: 2
              Label:
                  text: "IP"
  
              TextInput:
                  id: ip
                  multiline: False
  
              Label:
                  text: "PORT"
  
              TextInput:
                  id: port
                  multiline: False
  
              Label:
                  text: "Speed"
  
              TextInput:
                  id: speed
                  multiline: False
  
              Label:
                  text: "Angle"
  
              TextInput:
                  id: angle
                  multiline: False
  
          GridLayout:
              cols: 2
              Button:
                  text: "CONNECT"
                  on_press: root.connecting()
  
              Button:
                  text: "DISCONNECT"
                  on_press: root.dct()
  
          GridLayout:
              cols: 2
  
              Button:
                  text: "SEND DATA"
                  on_press: root.send_data()
  
              Button:
                  text: "STOP SERVER"
                  on_press: root.stop_srvv()
  
              Label:
                  id: my_label
                  text: ""
  </code></pre>
  
  <p>I want to connect, disconnect and send data through TCP connection, all of them with the server alive all the time (the server should close ONLY when I press the button "Stop server").
  Apparently the kivy module have a weird structure and I don't really understand how to assign multiprocessing in my problem AND kivy structure.</p>
  
  <p>Can somebody of you guys look at my code and correct it with multiprocessing to do what I need or at least give me an advice/tutorial or something?</p>
  
  <p><strong>UPDATE</strong>: I didn't used any multiprocessing code yet as you see, firstly I tried with pool and map to divide the connecting part to one core and rest of them on other, but to keep my server alive I need a loop sequence in the connecting zone. (my laptop have only 2 cores) </p>