xif
xif

Reputation: 343

Write a string when I wait for the return answer of a function

I have a function which handles and returns some data.

When I run my code, it take about 5 sec to return the answer. Is it possible to do something else at the same time? For instance, I'd like to write a simple line like "Loading...".

Would a thread work?

Upvotes: 0

Views: 86

Answers (2)

downhillFromHere
downhillFromHere

Reputation: 1977

You could use the built-in class thread.Thread, e.g.

import threading

def data_processing():
    pass
def print_some_jokes():
    pass

def do_both():
    t1 = threading.Thread(target=data_processing)
    t2 = threading.Thread(target=print_some_jokes)

    t1.start()
    t2.start()

    t1.join()
    t2.join()

both = threading.Thread(target=do_both)
both.start()
both.join()

Upvotes: 3

Mohammad Ghufran
Mohammad Ghufran

Reputation: 41

You could simply print the "Loading..." before entering the function or at the first line when you enter the function. However, this is not doing something "else". It is a sequential execution.

If you want to do something else in parallel, yes, threads would be the way to go.

You should post your code and perhaps tell us what exactly is it that you want to do for a precise answer!

Upvotes: 1

Related Questions