擅长:python、mysql、java
<p>您可以通过多线程来实现这一点。您的函数随时间运行,两个函数在函数运行完成后立即终止:</p>
<pre><code>import threading
import time
def calc(): #this function generates the square of all numbers between 1 and 56000000
for i in range(1,56000000):
i*i
t1 = threading.Thread(target=calc)
t1.start() #starting a thread with the calc function
i = 1
while t1.is_alive(): #Check if the thread is alive
time.sleep(1)# print time after every second
print(f'Time elapsed - {i}s')
i = i+1
t1.join() #terminate thread once calc function is done
print('Done!')
</code></pre>