简体   繁体   中英

Run two function concurrently

I have defined several functions.

def func1():
    '''something goes here'''

def func2():
    '''something goes here'''

def func3():
    '''something goes here'''

def func4():
    '''something goes here'''

So the question is: I want to run func1() always and other function( func2() , func3() , func4() ) should be available if we call the function while func1() running. I don't want func2() , func3() , func4() run unless call by user . How this can be done?. Here is what I've done so far

if __name__ == '__main__':
    Thread(target=func1()).start()

Here I started the function func1() . Mean while the function func1() running if user call the other functions it should run otherwise not

I've referred some threading and multi processing but still not able to get the answer. Is it possible? If so please guide me in a correct way.

Thanks in advance

threading.Timer should do the trick:

from threading import Timer

def func1():
    t = Timer(60.0, func2)
    t.start() #func2 will be called 60 seconds from now.

def func2():
    '''something goes here'''

def func3():
    '''something goes here'''

def func4():
    '''something goes here'''

From what you've specified, this is the code. func1() is executed by thread, and it keeps on executing. The user specified inputs trigger the remaining functions.

from threading import Thread
def func1():
    '''do something'''
    func1() #recursive call to keep it running

def func2():
    '''something goes here'''

def func3():
    '''something goes here'''

def func4():
    '''something goes here'''
    
if __name__ == '__main__':
    Thread(target=func1).start()
    while True:
        n = input("Enter Command: ")
        if n=="func2":
            func2()
        elif n=="func3":
            func3()
        # add your cases
        else:
            print("Invalid Input")

Supposing you're doing this in Python interactive console. If you do:

from threading import Timer

def func2(): 
    print("func2 called")

def func1(): 
    print("func1 called")

t = Timer(60.0, func2)
t.start()

Now the console is free and prompt is shown. func2 will be executed 60.0 seconds later, and you can call func1() or whatever you want. Look:

在此处输入图片说明

One obvious error in your code is that to start the thread you need

Thread(target=func1).start()

ie target should REFER to the function NOT call it (not func1() ). So the code should read:

from threading import Thread
import time

def func1():
    while True:
        print('Running func1')
        time.sleep(60)

def func2():
    '''something goes here'''
    print('func2 called')

def func3():
    '''something goes here'''
    print('func3 called')

def func4():
    '''something goes here'''
    print('func4 called')


if __name__ == '__main__':
    Thread(target=func1).start()
    func2()
    func3()
    func4()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM