[Tutor] continuouse loop

Alan Gauld alan.gauld at btinternet.com
Thu Jul 17 21:28:53 CEST 2008


"Monika Jisswel" <monjissvel at googlemail.com> wrote

> Would a program using a continuouse loop such as in this code take 
> up
> resources on the system if left for long period ?

Any running program takes up some resources but whether this
one would increase its resource usage over time, which I assume
is what you want to know, would depend on what it did with self.data
and what happened in self.function_1.

If function_1 did nothing that was resource intensive - like build
a big list in memory or open a new file each time it was called
(and not release it) - then it would be fine. But if function_1 stored
data in a list or opened a new comms port on each call then yes
it will eat up resources.

> import sys
>>
>> while 1:
>>     self.data = sys.stdin.readline()
>>     self.function_1(data)

> What are my other options is I want to have a running program & 
> other
> programs communicate with it & get responses from it ?

The trick to writing long running processes such as Windows services
and Unix daemons is to ensure they are either stateless (the create
use and free the needed resources in each operation) or utilise pools
(pre-allocated sets of resources that are allocated to a function as
needed and released by the function when done - if you run out of
pool you take a decision to enlarge the pool or to stop servicing
requests until resource becomes available - possibly using a
queue if instant response is not critical)

Thee are framweworks around, such as twisted, that help with these
tasks.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list