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