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的...
感謝您的答案!