简体   繁体   English

完成后,Python程序不会退出

[英]Python program doesn't quit when finished

I have the following script 186.py: 我有以下脚本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 "Done" but python doesn't exit when this happens. 最后一行打印“完成”,但发生这种情况时python不会退出。 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 have to ctrl+C to get the time: 但我必须按ctrl + C来获取时间:

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

After the program outputs "Done" python CPU usage is very little... but the memory usage continues to grow... I used ctrl+C once it got to 80% of my system memory (its an old system). 在程序输出“Done”之后,python的CPU使用率非常低......但是内存使用量继续增长...一旦达到80%的系统内存(它是一个旧系统),我使用了ctrl + C.

What is going on here? 这里发生了什么? What is the program doing after Done is printed? 打印完成后程序正在做什么? Shouldn't it be done? 不应该这样做吗?

Thanks, Dan 谢谢,丹

I ran the same code on my 2 GHz dual-core laptop with 2GB RAM and it took about 1 1/2 minutes in Cygwin. 我在配备2GB RAM的2 GHz双核笔记本电脑上运行相同的代码,在Cygwin中花了大约1 1/2分钟。 The memory usage got up over 600 MB before the program quit and it took about 2-4 seconds after Done appeared for the prompt to come up and the memory to be released. 在程序退出之前,内存使用量增加了600 MB以上,在Done出现提示出现并释放内存后花了大约2-4秒。 However, I didn't see any memory increase after the Done appeared. 然而,在Done出现后我没有看到任何内存增加。

My guess is it has to do with memory management. 我的猜测是它与内存管理有关。 After the Done appears, Python is working on freeing all of the memory which might take quite a while on an older machine with less RAM. Done之后,Python正在努力释放所有内存,这可能需要一段时间才能在RAM较少的旧机器上运行。 I'm not sure why the memory actually increases unless there is just a delay in whatever is telling you how much memory is being used. 我不确定为什么内存实际上会增加,除非有任何告诉你正在使用多少内存的延迟。

There are no indications in what you've posted that match the described symptoms. 没有迹象表明您发布的内容符合所描述的症状。 Perhaps the indentation in the executed code is stuffed and you are really about to run another loop around the whole pile. 也许执行代码中的缩进是填充的,你真的要围绕整个堆运行另一个循环。 Note that only brave people are going to bother trying to reproduce your problem when it takes 34 minutes to run. 请注意,只有勇敢的人才会在需要34分钟的时间内尝试重现您的问题。 Can you reproduce this problem in a shorter time? 你能在更短的时间内重现这个问题吗?

When you did Control-C, what did the traceback say? 当你做Control-C时,追溯说了什么?

In any case, I'd strongly advise not having hard-coded constants(?) all over the place eg 524287 .... give it a meaningful name and do meaningful_name = 524287 at the start. 无论如何,我强烈建议不要在整个地方都使用硬编码常量(?),例如524287 ....给它一个有意义的名字并在开始时做meaningful_name = 524287名称meaningful_name = 524287 Or if it's really a variable, feed it in via sys.argv. 或者,如果它确实是一个变量,请通过sys.argv.

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

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