0

我在“.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 条指令的“设置输入”块重复了大约 900 次,因此该文件很长但易于理解。它只是给一些变量一些值并等待 250 毫秒。

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

4

3 回答 3

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.
于 2013-11-13T18:30:52.620 回答
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())

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

于 2013-11-13T18:23:17.460 回答
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())
于 2013-11-13T18:23:41.813 回答