2015-10-02 217 views
0

我有蟒蛇守護進程創造了蟒蛇守護packagePython的多線程守護進程(模塊守護進程,併線程一起使用)

我的應用程序工作正常,但現在我想讓它多線程。

該應用程序是從this example構建的。

我明白如何使用模塊線程。

但是如何一起使用它們呢?

我需要在不同的線程中運行我的應用程序的方法run()。 阿卡

def run(self): 
    # run threads. 
    while True: 

因此,只有主線程確實喜歡鎖定文件等,以及其他線程的所有守護的東西只是做並行一些額外的工作。

這可能嗎?以及如何做到這一點?

更新。也許multiprocessing模塊比線程更好呢?

回答

0

你確定你需要一個特定於python的守護進程嗎?一些工具的存在構成任何循環程序爲守護程序(啓動 - 停止守護想到)

#!/bin/bash 

### BEGIN INIT INFO 
# Provides:   <whatever> 
# Required-Start: $remote_fs $syslog 
# Required-Stop:  $remote_fs $syslog 
# Default-Start:  2 3 4 5 
# Default-Stop:  0 6 
# Short-Description: Start daemon at boot time 
# Description:  <whatever> auto start. 
### END INIT INFO 

#set -x 
set -e 

if [ "$1" = "start" ] 
    then 
     start-stop-daemon --start --startas /usr/bin/python3 --pidfile /tmp/<whatever>.pid --make-pidfile --background --chdir '/var/local/<whatever>/' -- <whatever>.py 

elif [ "$1" = "stop" ] 
    then 
     start-stop-daemon --stop --signal 2 --pidfile /tmp/<whatever>.pid 
fi 

否則,如果你真的需要它,那麼恐怕我不明白你的問題,你可以簡單地啓動像這樣

import threading 
w = threading.Thread(target=run, args=(<args>, <args>, <args>)) 
w.setDaemon(True) 
w.start() 
#code 

其他線程,如果你想在多進程的方式運行代碼,你可以像這樣

import multiprocessing 
p = multiprocessing.Process(target=run, args=(<args>, <args>, <args>)) 
p.start() 
p.join() 
#code