简体   繁体   English

运行连续的Python进程和内存管理

[英]Running a continuous Python process and memory management

My current assignment is to run a Python program continuously, it is going to be a cron job kind of thing, internally it will have objects which is going to be updated every 24hrs and then basically write the details in a file. 我当前的任务是连续运行Python程序,这将是一项Cron工作,内部将有对象每24小时更新一次,然后将详细信息基本上写入文件中。

Some advice required about the memory management 有关内存管理的一些建议

  1. Should I use single process or multi threaded. 我应该使用单进程还是多线程。 As there is a scope in the program which can be done parallel. 因为程序中有一个范围可以并行完成。 As it is going to run continuously some clarification would be required about the memory consumption of these threads also do I need to cleanup the resources of the threads after each execution. 由于它将连续运行,因此需要对这些线程的内存消耗进行一些澄清,我是否还需要在每次执行后清理线程的资源。 Is there any clean up method available for threads in python. python中是否有任何适用于线程的清理方法。

  2. When I do a object allocation in Python, do I need to think about the destructor as well or Python will do the gc. 当我在Python中进行对象分配时,我是否还需要考虑析构函数,否则Python将完成gc。

Please share your thoughts on this as well as what would be the best approach. 请分享您对此的想法以及最佳方法。

There seems to be a misunderstanding in your question. 您的问题似乎存在误解。

A cron job is a scheduled task that runs at a given interval of time. cron作业是在给定时间间隔运行的计划任务。 A program running continuously doesn't need to be scheduled, aside from being launched at boot. 除了在启动时启动之外,不需要计划连续运行的程序。

First, multi-threading in Python suffers the GIL , so unless you are calling multi-threading aware library functions or your computations are I/O bound (often blocked by input/output such as disk access, network access and such) that releases the GIL, you will only have an insubstantial gain by using threading. 首先,Python中的多线程会受到GIL的影响 ,因此除非您调用支持多线程的库函数,否则您的计算受I / O约束(通常受输入/输出(例如磁盘访问,网络访问等)阻止)会释放GIL,通过使用线程,您只会获得微不足道的收益。 You should though consider using the multiprocessing package for parallel computing. 但是,您应该考虑将多处理程序包用于并行计算。 Other options are NumPy-based calculations when the library is compiled with OpenMP or using a task-based parallel framework such as SCOOP or Celery . 其他选项是使用OpenMP或使用基于任务的并行框架(例如SCOOPCelery )编译库时,基于NumPy的计算。

As stated in the comments, memory management is built in in Python and you won't have to worry about it apart from deleting unused instances or elements. 如评论中所述,内存管理是内置于Python中的,除了删除未使用的实例或元素外,您不必担心它。 Python will garbage collect your program automatically for every element that doesn't have any variable bound to it, so be sure to delete them or let them fall off-scope accordingly. Python将为没有绑定到变量的每个元素自动垃圾收集程序,因此请确保删除它们或让它们相应地脱离作用域。

On a side-note, be careful with objects destructors in Python, they tend to exhibit a different behavior than other Object Oriented languages. 附带说明一下,请小心Python中的对象析构函数,它们倾向于表现出与其他面向对象语言不同的行为。 I recommend you reading of this matter before using them. 我建议您在使用它们之前先阅读此事。

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

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