Python:运行一个函数,直到另一个函数完成

Sim*_*eth 10 python background function

我有两个功能,draw_ascii_spinnerfindCluster(companyid).

我想要:

  1. findCluster(companyid)在后台运行并在处理....
  2. 运行draw_ascii_spinner直到findCluster(companyid)完成

我该如何开始尝试解决这个问题(Python 2.7)?

Sve*_*ach 11

使用线程:

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]
Run Code Online (Sandbox Code Playgroud)


nmi*_*els 7

您可以使用多处理.或者,如果findCluster(companyid)有明智的停止点,你可以把它变成一个发电机draw_ascii_spinner,做这样的事情:

for tick in findCluster(companyid):
    ascii_spinner.next()
Run Code Online (Sandbox Code Playgroud)