繁体   English   中英

Python停止读取.py脚本

[英]Python stops reading .py script

我在“ .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次,因此该文件相当长,但易于理解。 它只是为某些变量提供一些值,并等待250ms。

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

你真的应该调整你的计划,这样你就不需要再重复自己,经常。 即使每次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())

本质上,您需要将所有输入参数存储在一个大列表中,并指定一次。 然后遍历值,并编写块一次调用这些函数。 这还将防止您在文件深处的调用中遇到任何键入错误。 因此,这可能已经解决了您的问题。

您在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())

我建议在此使用内存分析器。 您可能内存不足。 或者输入可能与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.

暂无
暂无

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

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