2015-08-28 48 views
1

我知道這個問題已經被問到。但我不能一個答案極其像一個魅力...循環不支持多處理和睡眠

所以我有一個過程,我執行一個while循環。但它不會循環。它轉了一圈,之後......我不知道。這是我的循環:

import os 
import time 
import signal 
import getdata 
import twitter 
import multiprocessing 

collector = getdata.collectData(0) # Pour récupérer les données 
botter = twitter.twitter() # Pour tweeter 


def data_update_loop(): 
    while True: 
     print 'Tour de boucle!' 
     start = time.time() # On calcule le temps d'éxecution 

     global collector, botter 

     collector.write_data() 
     botter.hourly_message() 

     done = time.time() # On stope le chrono 

     # On s'assure que le programme attende bien une demie-heure 
     time.sleep(1800 - (done - start)) 

def abort(): 
    f = open('RUNNING.txt', 'r') 
    process = f.readline() 
    process = filter(None, process.split(",")) 

    try: 
     for p in process: 
      os.kill(int(p), signal.SIGQUIT) 
    finally: 
     f.close() 
     os.remove('RUNNING.txt') 


def main(): 
    if not os.path.isfile('RUNNING.txt'): 
     f = open('RUNNING.txt', 'w+') 

     data_update = multiprocessing.Process(target=data_update_loop) 

     data_update.start() 

     # On écrit les PIDs pour pouvoir fermer les process après 
     f.write('{},{}'.format(data_update.pid, mentions_update.pid)) 
     f.close() 

     choice = raw_input('Press X to abort all processes: ') 
     if choice.upper() == 'X': 
      abort() 

    else: 
     print 'Please restart the programm.' 
     os.remove('RUNNING.txt') 



if __name__ == '__main__': 
    try: 
     main() 
    except KeyboardInterrupt: 
     abort() 

你能解釋一下爲什麼嗎?我知道這是關於CPU的...
感謝您的答案!

回答

2

我沒有深入分析你的程序,但原因是主流程不等待它的孩子完成。

0

謝謝您的回答,我想它的工作,但不,它仍然無法正常工作......

#!/usr/bin/env python 
# coding: utf-8 

import os 
import time 
import signal 
import getdata 
import twitter 
import multiprocessing 

collector = getdata.collectData(0) # Pour récupérer les données 
botter = twitter.twitter() # Pour tweeter 


def data_update_loop(): 
    while True: 
     start = time.time() # On calcule le temps d'éxecution 

     global collector, botter 

     collector.write_data() 
     botter.hourly_message() 

     done = time.time() # On stope le chrono 

     # On s'assure que le programme attende bien une demie-heure 
     time.sleep(1800 - (done - start)) 


def mentions_update_loop(): 
    global botter 
    botter.mentions() 


def abort(): 
    f = open('RUNNING.txt', 'r') 
    process = f.readline() 
    process = filter(None, process.split(",")) 

    try: 
     for p in process: 
      os.kill(int(p), signal.SIGQUIT) 
    finally: 
     f.close() 
     os.remove('RUNNING.txt') 


def main(): 
    if not os.path.isfile('RUNNING.txt'): 
     print 'Press ctrl + c to abort all processes.' 

     f = open('RUNNING.txt', 'w+') 

     data_update = multiprocessing.Process(target=data_update_loop) 
     mentions_update = multiprocessing.Process(target=mentions_update_loop) 

     data_update.start() 
     mentions_update.start() 

     # On écrit les PIDs pour pouvoir fermer les process après 
     f.write('{},{}'.format(data_update.pid, mentions_update.pid)) 
     f.close() 

    else: 
     print 'Please restart the programm.' 
     os.remove('RUNNING.txt') 



if __name__ == '__main__': 
    try: 
     main() 
    except KeyboardInterrupt: 
     abort() 

正如你說的,我更新了我的main()函數,它沒有做任何過程開始後...

感謝您的幫助!