[英]simple python script using too much cpu
最近,我的vps通知我,因為我的python腳本使用了太多的cpu(顯然,該腳本使用了整個核心幾個小時)。
我的腳本使用twython庫流推文
def on_success(self, data):
if 'text' in data:
self.counter += 1
self.tweetDatabase.save(Tweet(data))
#we only want to commit when we have a batch
if self.counter >= 1000:
print("{0}: commiting {1} tweets".format(datetime.now(), self.counter))
self.counter = 0
self.tweetDatabase.commit()
Tweet是一個類,其工作是丟棄有關我不需要的Tweet的元數據:
class Tweet():
def __init__(self, json):
self.user = {"id" : json.get('user').get('id_str'), "name" : json.get('user').get('name')}
self.timeStamp = datetime.datetime.strptime(json.get('created_at'), '%a %b %d %H:%M:%S %z %Y')
self.coordinates = json.get('coordinates')
self.tweet = {
"id" : json.get('id_str'),
"text" : json.get('text').split('#')[0],
"entities" : json.get('entities'),
"place" : json.get('place')
}
self.favourite = json.get('favorite_count')
self.reTweet = json.get('retweet_count')
它還具有__str__
方法,該方法將返回對象的超緊湊字符串表示形式
tweetDatabase.commit()
僅將tweet保存到文件,而tweetDatabase.Save()
僅將tweet保存到列表:
def save(self, tweet):
self.tweets.append(tweet.__str__())
def commit(self):
with open(self.path, mode='a', encoding='utf-8') as f:
f.write('\n'.join(self.tweets))
self.tweets = []
保持CPU低的最佳方法是什么? 如果我睡着了,我將失去推文,因為那將是該程序花費在不收聽twitters api上的時間。 盡管這樣,我在程序寫入文件后嘗試睡了一秒鍾,但這並沒有降低CPU的性能。 為了將記錄保存到文件中,每1000條鳴叫僅需一分鍾。
非常感謝
您可以嘗試使用以下方法對程序進行性能分析
import cProfile
command = """<whatever line that starts your program>"""
cProfile.runctx( command, globals(), locals(), filename="OpenGLContext.profile" )
然后使用RunSnakeRun( http://www.vrplumber.com/programming/runsnakerun/ )查看OpenGLContext.profile
塊越大,該功能占用的CPU時間就越多。 這將幫助您准確確定程序的哪一部分占用大量CPU
嘗試檢查是否需要先在on_success()中提交。 然后,檢查該推文是否包含您要保存的數據。 您可能還需要考慮self.counter變量上的競爭條件,並且應該將self.count的更新內容包裝在互斥鎖或類似內容中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.