下面是我的代码的很大一部分,基本上,如果您向下滚动到该execute_subscripts()
函数,您可以看到我运行了两个脚本,它们运行良好execfile
,它们显示prints
,它们将traceback
错误保存到错误文件中。
我正在尝试将第二个脚本变成一个不等待自己完成然后再转到下一个脚本的脚本。
如您所见,我试图使用subprocess
withPopen
来启动一个无声的隐藏窗口......但是它似乎没有运行,我不知道如何p.communicate()
正确使用该函数来检索tracebacks
和/或prints
.
我还...需要帮助创建某种超时/终止开关,因此如果下标通过Popen
或execfile
路由未在 5 分钟内完成,它会跳过该循环或重试并跳过,如果它立即再次失败.
我知道我可能不应该使用strftime
这个时代......但是那部分对我来说很好,所以我认为没有必要改变它。
from datetime import date, timedelta
from sched import scheduler
from time import time, sleep, strftime
import random
import traceback
import subprocess
s = scheduler(time, sleep)
random.seed()
def periodically(runtime, intsmall, intlarge, function):
## Get current time
currenttime = strftime('%H:%M:%S')
## If currenttime is anywhere between 23:40 and 23:50 then...
if currenttime > '23:40:00' and currenttime < '23:50:00':
## Open the error logging file as the variable "errors"
errors = open('MISC/ERROR(S).txt', 'a')
## Try to...
try:
## Call the clear subscript.
execfile("SUBSCRIPTS/CLEAR.py", {})
## On exception (fail)...
except Exception:
## Write the entire traceback error to file...
errors.write(traceback.format_exc() + '\n')
errors.write("\n\n")
## Close and exit the error logging file.
errors.close()
## Update time
currenttime = strftime('%H:%M:%S')
## Idle time
while currenttime >= '23:40:00' and currenttime <= '23:59:59' or currenttime >= '00:00:00' and currenttime <= '11:30:00':
## Update time
currenttime = strftime('%H:%M:%S')
print currenttime, "Idling..."
sleep(10)
## Update time
currenttime = strftime('%H:%M:%S')
## Initiate the scheduler.
runtime += random.randrange(intsmall, intlarge)
s.enter(runtime, 1, function, ())
s.run()
def execute_subscripts():
st = time()
print "Running..."
errors = open('MISC/ERROR(S).txt', 'a')
try:
execfile("SUBSCRIPTS/TESTSCRIPT.py", {})
except Exception:
errors.write(traceback.format_exc() + '\n')
errors.write("\n\n")
try:
execfile("SUBSCRIPTS/TEST.py", {})
except Exception:
errors.write(traceback.format_exc() + '\n')
errors.write("\n\n")
## subprocess.Popen(["pythonw", "SUBSCRIPTS/TEST.py", "0"], shell=True)
try:
execfile("SUBSCRIPTS/TESTSCRIPTTest.py", {})
except Exception:
errors.write(traceback.format_exc() + '\n')
errors.write("\n\n")
try:
execfile("SUBSCRIPTS/TESTTESTTEST.py", {})
except Exception:
errors.write(traceback.format_exc() + '\n')
errors.write("\n\n")
errors.close()
print """The whole routine took %.3f seconds""" % (time() - st)
while True:
periodically(50, -25, +90, execute_subscripts)
任何想法将不胜感激
添加了赏金,希望有人知道如何实现这一目标。
在此先感谢
Hyflex
我希望脚本能够执行的操作示例...
下标 1 - 在后台运行,将打印和错误从 subscript1.py 发送到 main.py,不要等待它完成,转到下标 2,10秒后超时(或尽可能接近 10 秒,或超时在调用了所有下标之后。)
下标 2 - 在后台运行,将打印和错误从 subscript2.py 发送到 main.py,在进入下标 3 之前等待它完成, 10 秒后超时(或尽可能接近 10 秒,或者毕竟超时下标已被调用。)
下标 3 - 在后台运行,将打印和错误从 subscript3.py 发送到 main.py,在进入下标 4 之前等待它完成, 10 秒后超时(或尽可能接近 10 秒,或者毕竟超时下标已被调用。)
下标 4 - 在后台运行,将打印和错误从 subscript4.py 发送到 main.py,不要等待它完成,转到下标5,10 秒后超时(或尽可能接近 10 秒,或者调用所有下标后超时。)
下标 5 - 在后台运行,将打印和错误从 subscript5.py 发送到 main.py,等待它完成后再进入下一个下标(或者在这种情况下,循环结束), 10 秒后超时(或接近10 秒,或者在调用所有下标后超时。)
shx2 的打印和追溯
[pid=9940] main running command: C:\Python27\python.exe SUB/subscript1.py (is_bg=False)
[pid=9940] main running command: C:\Python27\python.exe SUB/subscript1.py (is_bg=True)
Traceback (most recent call last):
File "C:\Test\main.py", line 21, in <module>
bg_proc1 = run_subscript(cmd, is_bg = True)
File "C:\Test\main.py", line 10, in run_subscript
return (cmd > sys.stdout) & BG # run in background
File "C:\Python27\lib\site-packages\plumbum\commands\modifiers.py", line 81, in __rand__
return Future(cmd.popen(), self.retcode)
File "C:\Python27\lib\site-packages\plumbum\commands\base.py", line 317, in popen
return self.cmd.popen(args, **kwargs)
File "C:\Python27\lib\site-packages\plumbum\commands\base.py", line 233, in popen
return self.cmd.popen(self.args + list(args), **kwargs)
File "C:\Python27\lib\site-packages\plumbum\machines\local.py", line 104, in popen
**kwargs)
File "C:\Python27\lib\site-packages\plumbum\machines\local.py", line 253, in _popen
stderr = stderr, cwd = str(cwd), env = env, **kwargs) # bufsize = 4096
File "C:\Python27\lib\subprocess.py", line 703, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "C:\Python27\lib\subprocess.py", line 851, in _get_handles
c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
UnsupportedOperation: fileno
编辑:
| --> # Sub 1.py # --> Sequential with timeout --> Started: 11:30.00 --> Estimated Completion: 11:30.01 (1 Second) --> Timeout at 11:30:10 (10 Seconds) --> # Sub 2.py # --> Sequential with timeout --> Started: 11:30.02 (or after time Sub 1.py's timeout) --> Estimated Completion: 11:30.03 (1 Second) --> Timeout at 11:30:13 (10 Seconds) --> # Sub 3.py # --> Sequential with timeout --> Started: 11:30.04 (or after time Sub 2.py's timeout) --> Estimated Completion: 11:30.08 (3 Seconds) --> Timeout at 11:30:18 (10 Seconds)
| ^ ^
| | |
| -------------------------------------------------------------------------------------------------------------------------------------------------- |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
Scheduler -->|
| --> Sub 4.py --> Nonsequential with timeout --> Started: 11:30.00 --> Estimated Completion: 11:30.05 (5 Seconds) --> Timeout at 11:30:10 (15 Seconds)
|
| --> Sub 5.py --> Nonsequential with timeout --> Started: 11:30.00 --> Estimated Completion: 11:30.02 (2 Seconds) --> Timeout at 11:30:10 (10 Seconds)
|
| --> Sub 6.py --> Nonsequential with timeout --> Started: 11:30.00 --> Estimated Completion: 11:30.10 (10 Seconds) --> Timeout at 11:30:10 (25 Seconds)
希望这有助于直观地表示我想要实现的目标