code: def _run(self, device_url, code):
          
          try:
  
              print("DEBUG: run code\n%s" % code)
              env = os.environ.copy()
              env['UIAUTOMATOR_DEBUG'] = 'true'
              if device_url and device_url != 'default':
                  env['ATX_CONNECT_URL'] = tostr(device_url)
              start_time = time.time()
  
              self.proc = subprocess.Popen([sys.executable, "-u"],
                                           env=env, stdout=PIPE, stderr=subprocess.STDOUT, stdin=PIPE)
              self.proc.stdin.write(code)
              self.proc.stdin.close()
  
              for line in iter(self.proc.stdout.readline, b''):
                  print("recv subprocess:", repr(line))
                  if line is None:
                      break
                  gqueue.put((self, {"buffer": line.decode('utf-8')}))
              print("Wait exit")
              exit_code = self.proc.wait()
              duration = time.time() - start_time
              ret = {
                  "buffer": "",
                  "result": {"exitCode": exit_code, "duration": int(duration)*1000}
              }
              gqueue.put((self, ret))
              time.sleep(3)  # wait until write done
          except Exception:
              traceback.print_exc()
docs: Thanks: https://gist.github.com/mosquito/e638dded87291d313717