Python: run one function until another function finishes

Question:

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)?

Asked By: Simply Seth

||

Answers:

You can use multiprocessing. Or, if findCluster(companyid) has sensible stopping points, you can turn it into a generator along with draw_ascii_spinner, to do something like this:

for tick in findCluster(companyid):
    ascii_spinner.next()
Answered By: nmichaels

Run findCluster() in a thread (the Threading module makes this very easy), and then draw_ascii_spinner until some condition is met.

Instead of using sleep() to set the pace of the spinner, you can wait on the thread’s wait() with a timeout.

Answered By: slezica

This can be done with threads. FindCluster runs in a separate thread and when done, it can simply signal another thread that is polling for a reply.

Answered By: Spyros

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]
Answered By: Sven Marnach

You’ll want to do some research on threading, the general form is going to be this

  • Create a new thread for findCluster and create some way for the program to know the method is running – simplest in Python is just a global boolean
  • Run draw_ascii_spinner in a while loop conditioned on whether it is still running, you’ll probably want to have this thread sleep for a short period of time between iterations

Here’s a short tutorial in Python – http://linuxgazette.net/107/pai.html

Answered By: dfb

Generally, you will use Threads. Here is a simplistic approach which assumes, that there are only two threads: 1) the main thread executing a task, 2) the spinner thread:

#!/usr/bin/env python

import time
import thread

def spinner():
    while True:
        print '.'
        time.sleep(1)

def task():
    time.sleep(5)

if __name__ == '__main__':
    thread.start_new_thread(spinner, ())
    # as soon as task finishes (and so the program)
    # spinner will be gone as well
    task()
Answered By: miku

It is possible to have a working example? I am new in Python. I have 6 tasks to run in one python program. These 6 tasks should work in coordinations, meaning that one should start when another finishes. I saw the answers , but I couldn’t adopted the codes you shared to my program.
I used "time.sleep" but I know that it is not good because I cannot know how much time it takes each time.

# Sending commands
for i in range(0,len(cmdList)):             # port Sending commands
cmd = cmdList[i]
cmdFull = convert(cmd)
port.write(cmd.encode('ascii'))

# s = port.read(10)

print(cmd)

# Terminate the command + close serial port

port.write(cmdFull.encode('ascii'))
print('Termination')
port.close()

# time.sleep(1*60)
Answered By: Soleil
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.