简体   繁体   English

Python Google Calendar API内存问题

[英]Python Google Calendar API memory issue

I am using the reference Python code from Google (link below) which uses the Google Calendar API to get the next 10 events from my Gmail calendar. 我正在使用Google的参考Python代码(下面的链接),该代码使用Google Calendar API从我的Gmail日历中获取接下来的10个事件。 This is working fine. 一切正常。

However, my end goal is to have the calendar integrated for new events every 30 minutes. 但是,我的最终目标是每30分钟为新事件集成一次日历。 As such I have been running the code in a loop and I have noticed that each time the check is run the system memory used increases. 因此,我一直在循环运行代码,并且我注意到每次运行检查时,使用的系统内存都会增加。 Well I say every time but in fact it seems to be random. 我每次都说,但实际上似乎是随机的。 Sometimes it will run 50 times with no increase in footprint, other times it will increase every time. 有时它会运行50次而不会增加占用空间,而其他时候每次都会增加。 If run for long enough the app crashes. 如果运行足够长时间,则应用程序将崩溃。

I am running this on a Raspberry Pi if that is of any relevance. 如果有任何相关性,我将在Raspberry Pi上运行。 Sorry I confess to not being a developer, I really hope I am missing something obvious! 对不起,我承认自己不是开发人员,我真的希望我缺少明显的东西!

The code I am using came from here: https://developers.google.com/google-apps/calendar/quickstart/python#step_3_set_up_the_sample 我使用的代码来自这里: https : //developers.google.com/google-apps/calendar/quickstart/python#step_3_set_up_the_sample

The only changes I have made are to run the 'main' procedure in a loop as below: 我所做的唯一更改是如下循环运行“ main”过程:

while True:
    main()
    print ('Sleeping')
    sleep (60)

I have also inserted the following line of code to output the memory use at various points in the application: 我还插入了以下代码行,以输出应用程序中各个位置的内存使用情况:

print ('Memory usage at start: %s (kb)' % resource.getrusageresource.RUSAGE_SELF).ru_maxrss) #print memory usage

From the testing I have done it seems the memory use is increased by the service build: 根据我所做的测试,服务构建似乎增加了内存使用量:

service = discovery.build('calendar', 'v3', http=http)

I am looking for a way to stop the memory use getting out of hand, any help would be greatly appreciated. 我正在寻找一种方法来阻止内存使用失控,任何帮助将不胜感激。

Thank you in advance Tim 提前谢谢你蒂姆

Try removing the lines below out of the loop by removing it from main() since this is just used for initializing your application's authentication. 尝试通过将其从main()删除来将以下几行从循环中删除,因为这仅用于初始化应用程序的身份验证。

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)

Also, the parameter for sleep() is counted as seconds, use '1800' instead since you want it to run every 30 minutes. 另外, sleep()的参数以秒为单位,请改为使用“ 1800”,因为您希望它每30分钟运行一次。

You can try implementing your code like below 您可以尝试实现您的代码,如下所示

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
while True:
main()
print ('Sleeping')
sleep (1800)

Also, you can wrap your service object with weakref.proxy object. 此外,您可以使用weakref.proxy对象包装服务对象。

import weakref
# ... code
service = weakref.proxy(discovery.build('calendar', 'v3', http=http))

Works for me in multithreaded environment. 在多线程环境中为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM