Python停止读取.py脚本

时间:2013-11-13 18:09:18

标签: python python-3.x

我在“.py”文件中有一个python脚本,基本上包含这样的内容:

#import time and tcmodule.py module
import time
import tcmodule as tc

# Set Inputs (step 1):
tc.SetInput1(0)
tc.SetInput2(0)
tc.SetInput3(0)
time.sleep(0.250)
print(time.time())

# Set Inputs (step 2):
tc.SetInput1(3)
tc.SetInput2(6)
tc.SetInput3(12)
time.sleep(0.250)
print(time.time())

#the code continues the same way...

包含这4条指令的“Set Inputs”块重复约900次,因此文件很长但很容易理解。它只为某些变量提供了一些值,并等待250毫秒。

问题在于,当我执行程序时,pythonwin突然停止读取脚本(我知道因为它突然停止打印时间)而且我不知道为什么会发生这种情况。最奇怪的是它每次都停在不同的地方,所以我猜代码还可以。有人知道代码有什么问题吗?

3 个答案:

答案 0 :(得分:0)

你真的应该重组你的程序,所以你需要经常重复自己。即使SetInput调用的参数每次都不同,您仍然可以节省很多:

import time
import tcmodule as tc

inputs = [
    (0, 0, 0),
    (3, 6, 12),
    # more values here
]

for a, b, c in inputs:
    tc.SetInput1(a)
    tc.SetInput2(b)
    tc.SetInput3(c)
    time.sleep(0.250)
    print(time.time())

您基本上需要将所有输入参数存储在一个大列表中并指定一次;然后你循环遍历这些值,只需编写一个块,你可以在那里调用那些函数一次。这也可以防止在文件深处的调用中出现任何输入错误。所以这可能已经解决了你的问题。

答案 1 :(得分:0)

您可能在900次迭代中的某个地方出现了错字。我建议你这样做。这将使调试变得更加容易。

# create a list of lists
# each sublist will contain the three values that go with each SetInput function
# add as many sublists as necessary 
inputs = [[0, 0, 0], [3, 6, 12]]

for inp in inputs:
    print "step", inp
    tc.SetInput1(inp[0])
    tc.SetInput2(inp[1])
    tc.SetInput3(inp[2])
    time.sleep(0.250)
    print(time.time())

答案 2 :(得分:0)

我建议在此使用内存分析器。你可能内存不足。或者输入可能与tc.SetInput的预期不同。

#import time and tcmodule.py module
import time
import tcmodule as tc
from memory_profiler import profile

@profile
def my_func():
    tc.SetInput1(input1)
    tc.SetInput2(input2)
    tc.SetInput3(input3)
    time.sleep(0250)
    print(time.time())
def parseline():
   #take out the "pass" and return the 3 input values
   pass

with somefile.txt as txtfile:
   for line in file:
      try:
          input1,input2,input3=parseline(line)
          myfunc(input1,input2,input3)
      except Exception:
          #add error handling here and change the Exception.