Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: run one function until another function finishes

I have two functions, draw_ascii_spinner and findCluster(companyid).

I would like to:

  1. Run findCluster(companyid) in the backround and while its processing....
  2. Run draw_ascii_spinner until findCluster(companyid) finishes

How do I begin to try to solve for this (Python 2.7)?

like image 498
Simply Seth Avatar asked Mar 15 '11 18:03

Simply Seth


1 Answers

Use threads:

import threading, time

def wrapper(func, args, res):
    res.append(func(*args))

res = []
t = threading.Thread(target=wrapper, args=(findcluster, (companyid,), res))
t.start()
while t.is_alive():
    # print next iteration of ASCII spinner
    t.join(0.2)
print res[0]
like image 194
Sven Marnach Avatar answered Oct 21 '22 01:10

Sven Marnach