title: How to start separate process that runs a function with multiple arguments?
body: <p>So I'm having a though time wrapping my head around multiprocessing library and all the functionality. Basicly what I'm trying to accomplish is to start a separate process from a background thread that receives function object and it's positional and keyword arguments.</p>
  
  <p>I have a thread that is started at the beginning and it's job is to execute functions that are passed to it via dependency injection. Once the thread detects that new job is scheduled it takes the job and executes it. The problem is that I have no idea how long that job will take and I would like to terminate it if let's say 10 minutes have passed. Since this can't be accomplished via <code>threading</code> module I decided to take a look at <code>multiprocessing</code> since it's processes can be terminated. </p>
  
  <p>Dependency injection is solved via decorator that encapsulates each function (that is intended to be executed by the thread) that passes function object and it's positional and keyword arguments to the thread that's gonna execute it via <code>*</code> and <code>**</code>. The thread at the end gets all arguments and the function object (this works). </p>
  
  <p>The problem begins when i try to create a <code>Pool</code> and assing work to a single worker. Since I have no idea of function input arguments, how am I able to use <code>apply_async</code> functin with <code>*</code> and <code>**</code>?</p>
  
  <pre><code>def intercept(callback):
      def wrapper(*args, **kwargs):
          # pass callback, args and kwargs to the thread
          pass
      return wrapper
  
  @intercept
  def do_some_work(first, second, third=None):
      time.sleep(10)
  
  def bg_thread():
      while True:
          # acquire callback, args and kwargs from intercept decorator
          # if new job is scheduled create a process and execute it
          # if process did not finish in timeout, terminate it
          p = multiprocessing.Pool()
          ret = p.apply_async(callback, args, kwargs)
          p.close()
          try:
              ret.get(5)
          except:
              p.terminate()
  
  
  t = threading.Thread(target=bg_thread)
  t.start()
  do_some_work()
  </code></pre>