简体   繁体   中英

Python - Threading - execute simultaneously

I have this example code:

# some imports that I'm not including in the question

class daemon:
    def start(self):
        # do something, I'm not including what this script does to not write useless code to the question
        self.run()

    def run(self):
        """You should override this method when you subclass Daemon.

        It will be called after the process has been daemonized by 
        start() or restart().
        """

class MyDaemon(daemon):
    def run(self):
        while True:
            time.sleep(1)

if __name__ == "__main__":
    daemonz = MyDaemon('/tmp/daemon-example.pid')
    daemonz.start()

def firstfunction():
    # do something
    secondfunction()

def secondfunction():
    # do something
    thirdfunction()

def thirdfunction():
    # do something

# here are some variables set that I am not writing
firstfunction()

How can I exit from the run(self) function of class "daemon" and going on executing the firstfunction() like written in the last line? I'm a newbie with Python, and I'm trying to learn

# EDIT I managed to implement the daemon class into the treading class. But I'm in the same situation of first, the script stays in daemon class and doesn't execute the other lines.

class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def daemonize(self):
    # istructions to daemonize my script process

    def run(self):
        self.daemonize()


def my_function():
    print("MyFunction executed") # never executed

thread = MyThread()
thread.start()
my_function() # the process is successfully damonized but
              # this function is never executed

You may use the break keyword to exit loops, and continue to the next line. return can be used to exit functions.

class daemon:
    def start(self):
        self.run()

    def run(self):
        while True:
            break
        return
        print()  # This never executes

If you want MyDaemon to run alongside the rest of your code, you have to make it a process or thread. Code then automatically continues to the next line, while the MyDaemon class (thread/process) runs.

import threading  


class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        print("Thread started")
        while True:
            pass


def my_function():
    print("MyFunction executed")

thread = MyThread()
thread.start()        # executes run(self)
my_function()

This code produces the following result:

Thread started
MyFunction executed

To make thread a daemon, you can use thread.setDaemon(True) . That function must be called before the thread is started:

thread = MyThread()
thread.setDaemon(True)
thread.start()
my_function()

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