Python:线程或多处理? [英] Python: threading or multiprocessing?

查看:110
本文介绍了Python:线程或多处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



我需要每15秒对文件进行一次简短的写入(并休息剩下的时间)...在我看来,多线程或多处理将有助于解决这个问题,通过专门的线程或进程来执行文件写入。在时间/可靠性和内存占用方面哪个更好?

背景



我是为Chumby编写一个小型Python应用程序(因此有限的内存可用性 - 总共128MB的系统内存);在杀死它之后停止默认的Chumby控制面板重新启动,需要每15秒钟写一个临时文件来愚弄通常会重新启动控制面板的看门狗进程。主要应用程序可能忙于做其他事情,我不想尝试看钟,因为它正在做其他事情,以确保它在临时文件写入。

$ b $ chumby似乎是基于Linux的,所以

应该可以使用(.setitimerrel =nofollow> signal.setitimer() (只要你有权访问Python 2.6或更高版本)。



这个函数允许你安装一个定期调用的处理函数,所以你不需要一个线程或进程。

Question

I need to make a short write to a file every 15 seconds (and sleep the rest of the time)... it seems to me that multithreading or multiprocessing would be useful to address this, by having a dedicated thread or process to do the file write. Which would be better in terms of timing/reliability, as well as memory footprint?

Background

I am writing a small Python application for a Chumby (so limited memory availability -- 128MB total system memory); to stop the default Chumby control panel from restarting after I've killed it, a temp file needs to be written to every 15 seconds or so to "fool" the watchdog process that would ordinarily restart the control panel. The main application may be busy doing other things, and I don't want to try to have to "watch the clock" as it's doing its other things to make sure it squeezes in the temp file write.

解决方案

The Chumby seems to be Linux-based, so signal.setitimer() should be available (provided you have access to Python 2.6 or above).

This function allows you to install a handler that is periodically called, so you don't need a thread or process.

这篇关于Python:线程或多处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆