7

Do you guys have any recommendations on what python modules to use for the following application: I would like to create a daemon which runs 2 threads, both with while True: loops.

Any examples would be greatly appreciated! Thanks in advance.

Update: Here is what I have come up with, but the behavior is not what I expected.

import time
import threading

class AddDaemon(object):
    def __init__(self):
        self.stuff = 'hi there this is AddDaemon'

    def add(self):
        while True:
            print self.stuff
            time.sleep(5)


class RemoveDaemon(object):
    def __init__(self):
        self.stuff = 'hi this is RemoveDaemon'

    def rem(self):
        while True:
            print self.stuff
            time.sleep(1)

def run():
    a = AddDaemon()
    r = RemoveDaemon()
    t1 = threading.Thread(target=r.rem())
    t2 = threading.Thread(target=a.add())
    t1.setDaemon(True)
    t2.setDaemon(True)
    t1.start()
    t2.start()
    while True:
        pass

run()

output

Connected to pydev debugger (build 163.10154.50)
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon
hi this is RemoveDaemon

It looks like when I try to create a thread object using:

t1 = threading.Thread(target=r.rem())
t2 = threading.Thread(target=a.add())

the while loop in r.rem() is the only one that gets executed. What am I doing wrong?

dobbs
  • 1,089
  • 6
  • 22
  • 45
  • lots of options... but `multiprocessing.Pool.ThreadPool` is a good one. That `while True`.... will the code it runs tend to block or is it cpu intensive? Child processes may be needed. – tdelaney Feb 28 '17 at 02:54
  • `ThreadPool` isn't a good idea if the threads are persistent; `Pool`s are intended for discrete tasks, generally speaking... – ShadowRanger Feb 28 '17 at 03:01
  • 1
    I just did this a few weeks ago. A similar thing. I used `concurrent.futures` and `supervisord` as my daemon tool. But you can also use `pm2`. I was building a chat bot and used multiple threads to handle large streams. – Ali Gajani Feb 28 '17 at 03:07
  • @AliGajani would you be willing to share a quick little example of how you used the two together? – dobbs Feb 28 '17 at 16:12
  • 1
    @dobbs really you just had a typo... what you had was 99.9% the way there. – Aaron Mar 01 '17 at 21:20

1 Answers1

6

When you are creating your threads t1 and t2, you need to pass the function not call it. when you call r.rem(), it enters the infinite loop before you create the thread and separate it from the main one. the solution to this is to remove the parenthesis from r.rem() and a.add() in your thread constructors.

import time
import threading

class AddDaemon(object):
    def __init__(self):
        self.stuff = 'hi there this is AddDaemon'

    def add(self):
        while True:
            print(self.stuff)
            time.sleep(3)


class RemoveDaemon(object):
    def __init__(self):
        self.stuff = 'hi this is RemoveDaemon'

    def rem(self):
        while True:
            print(self.stuff)
            time.sleep(1)

def main():
    a = AddDaemon()
    r = RemoveDaemon()
    t1 = threading.Thread(target=r.rem)
    t2 = threading.Thread(target=a.add)
    t1.setDaemon(True)
    t2.setDaemon(True)
    t1.start()
    t2.start()
    time.sleep(10)

if __name__ == '__main__':
    main()
Aaron
  • 10,133
  • 1
  • 24
  • 40