fork download
  1. from threading import Thread
  2. from time import sleep
  3. from Queue import Queue
  4.  
  5. def firstresult(*functions):
  6. queue = Queue()
  7. threads = []
  8. for f in functions:
  9. def thread_main():
  10. queue.put(f())
  11. thread = Thread(target=thread_main)
  12. threads.append(thread)
  13. thread.start()
  14. result = queue.get()
  15. return result
  16.  
  17. def slow():
  18. sleep(1)
  19. return 42
  20.  
  21. def fast():
  22. return 0
  23.  
  24. if __name__ == '__main__':
  25. print firstresult(slow, fast)
Success #stdin #stdout 0.1s 25272KB
stdin
Standard input is empty
stdout
0