Python program does not exit

I have the following script 186.py:

S=[] study=set([524287]) tmax=10**7 D={} DF={} dudcount=0 callcount=0 def matchval(t1,t2): if t1==t2: global dudcount dudcount+=1 else: global callcount callcount+=1 D.setdefault(t1,set([])) D.setdefault(t2,set([])) D[t1].add(t2) if t1 in D[t2]: DF.setdefault(t1,set([])) DF[t1].add(t2) DF.setdefault(t2,set([])) DF[t2].add(t1) for k in xrange(27): t1=(100003 - 200003*(2*k+1) + 300007*(2*k+1)**3)%(1000000) S.append(t1) t2=(100003 - 200003*(2*k+2) + 300007*(2*k+2)**3)%(1000000) S.append(t2) matchval(t1,t2) t1=(100003 - 200003*(55) + 300007*(55)**3)%(1000000) S.append(t1) t2=(S[31]+S.pop(0))%(1000000) S.append(t2) matchval(t1,t2) for k in xrange(29,tmax+1): t1=(S[31]+S.pop(0))%(1000000) S.append(t1) t2=(S[31]+S.pop(0))%(1000000) S.append(t2) matchval(t1,t2) D.setdefault(524287,set([])) DF.setdefault(524287,set([])) print D[524287] print DF[524287] print dudcount,callcount print "Done" 

The last line prints "Finish", but python does not exit when this happens. I type the following command:

 $ time python 186.py 

And get the results:

 set([810528L, 582178L, 49419L, 214483L, 974071L, 651738L, 199163L, 193791L]) set([]) 11 9999989 Done 

But I need ctrl + C to get the time:

 real 34m18.642s user 2m26.465s sys 0m11.645s 

After the "Done" program was released, the python processor load is very small ... but memory usage continues to grow ... I used ctrl + C when it reached 80% of my system memory (its old system).

What's going on here? What does the program do after creating Done? Shouldn't this be done?

Thanks Dan

+4
source share
2 answers

I ran the same code on a dual-core dual-core laptop with 2 GB of RAM and took about 1 1/2 minutes in Cygwin. Memory usage increased by more than 600 MB before the program terminated, and it took about 2-4 seconds after Done to prompt and exit the memory. However, after the appearance of Done I did not see an increase in memory.

I assume this is related to memory management. Since the advent of Done Python has been working to free up all memory, which can take quite a while on an older machine with less RAM. I'm not sure why the memory is really increasing, unless there is a delay in what tells you how much memory is being used.

+5
source

In what you published, there are no indications corresponding to the described symptoms. Perhaps the indentation in the executable code is full, and you're really going to run another loop around the entire heap. Please note that only brave people will try to reproduce your problem when it needs 34 minutes. Can you reproduce this problem in a shorter time?

When you did Control-C, what did the trace say?

In any case, I highly recommend not having hardcoded constants (?) All over the place, for example. 524287 .... give it a meaningful name and make meaningful_name = 524287 from the start. Or, if it's really a variable, pass it through sys.argv.

+1
source

Source: https://habr.com/ru/post/1303601/


All Articles