title: Python multiprocessing with async functions
body: <p>I built a websocket server, a simplified version of it is shown below:</p>
  
  <pre><code>import websockets, subprocess, asyncio, json, re, os, sys
  from multiprocessing import Process
  
  def docker_command(command_words):
      return subprocess.Popen(
          ["docker"] + command_words,
          stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
  
  async def check_submission(websocket:object, submission:dict):
      exercise=submission["exercise"]
      with docker_command(["exec", "-w", "badkan", "grade_exercise", exercise]) as proc:
          for line in proc.stdout:
              print("&gt; " + line)
              await websocket.send(line)
  
  async def run(websocket, path):
      submission_json = await websocket.recv()   # returns a string
      submission = json.loads(submission_json)   # converts the string to a python dict
  
      ####
      await check_submission(websocket, submission)
  
  
  websocketserver = websockets.server.serve(run, '0.0.0.0', 8888, origins=None)
  asyncio.get_event_loop().run_until_complete(websocketserver)
  asyncio.get_event_loop().run_forever()
  </code></pre>
  
  <p>It works fine when there is only a single user at a time. But, when several users try to use the server, the server processes them serially so later users have to wait a long time.</p>
  
  <p>I tried to convert it to a multiprocessing server by replacing the line marked with "####" ("await check_submission...") with:</p>
  
  <pre><code>p = Process(target=check_submission, args=(websocket, submission,))
  p.start()
  </code></pre>
  
  <p>But, it did not work - I got a Runtime Warning: "coroutine: 'check_submission' was never awaited", and I did not see any output coming through the websocket.</p>
  
  <p>I also tried to replace these lines with:</p>
  
  <pre><code>loop = asyncio.get_event_loop()
  loop.set_default_executor(ProcessPoolExecutor())
  await loop.run_in_executor(None, check_submission, websocket, submission)
  </code></pre>
  
  <p>but got a different error: "can't pickle asyncio.Future objects".</p>
  
  <p>How can I build this multi-processing websocket server?</p>