IIS启动的Python进程在一段时间后停止运行 [英] Python process started by IIS stops running after awhile

查看:226
本文介绍了IIS启动的Python进程在一段时间后停止运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,感谢阅读!



我写了一个Python脚本,它收集来自给定网页的链接。您可以在下面找到代码段。 (但我认为这不是编码问题)



当我在命令行中执行脚本(具有管理员权限)时,一切都按预期工作,但是当我运行来自PHP的脚本它在约30分钟后停止运行而没有任何异常。对我来说,看起来,Windows Server 2016在一段时间后就会杀死python进程。 (进程由没有管理员权限的IIS用户启动)



***任何想法如何告诉Windows / IIS不要杀死python进程?***



这就是我从PHP调用脚本的方式:



$ command ='C:\\Python36 \\python.exe C:\\Scripts\\myscript.py';

pclose(popen(start / B。$ command,w));



我试过python.exe和pythonw.exe

脚本正常启动,但在大约30分钟后停止。



def getAllLinksOnCurrentPage():

elems = driver.find_elements_by_xpath(// a [@href])
元素中的元素


f = elem.get_attribute(href)

如果f中的testur:

如果f不在linkcollector中:

linkcollector.append(f)





试试:

driver.get(testurl)



for currentpage i n linkcollector:

driver.get(当前页面)

getAllLinksOnCurrentPage()





#将所有URL写入Logfile

with open('C:\Scripts\Logfiles\\Logfile.txt',a)作为文件:
$ b linkcollector中当前页面的$ b:

file.write(\ n%s%currentpage)



除了例外e:

#Catch未知异常

打开('C:\Scripts\Logfiles\\'+ str(testrun)+'_ Logfile.txt', a)作为文件:

file.write(\ n未知异常:%str(e))



每次输入非常感谢,谢谢!



我的尝试:



- python.exe - >属性 - >以管理员身份运行

Hi there, thanks for reading!

I wrote a Python script which is collecting links from a given web page. You can find a code snippet below. (but I think it is not a coding issue)

When I execute the script in command line (with Administrator rights) everything works as expected, but when I run the script from PHP it just stops running after ~30 minutes without any Exception. For me it looks like, that Windows Server 2016 is killing the python process after a certain time. (process is started by IIS User without admin rights)

***Any ideas how to tell Windows/IIS not to kill the python process?***

This is how I call the script from PHP:

$command = 'C:\\Python36\\python.exe C:\\Scripts\\myscript.py ';
pclose(popen("start /B ".$command, "w"));

I tried both python.exe and pythonw.exe
The script is starting normally, but stops after about 30 minutes.

def getAllLinksOnCurrentPage():
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
f = elem.get_attribute("href")
if testurl in f:
if f not in linkcollector:
linkcollector.append(f)


try:
driver.get(testurl)

for currentpage in linkcollector:
driver.get(currentpage)
getAllLinksOnCurrentPage()


#Write all URLs to Logfile
with open('C:\Scripts\Logfiles\\Logfile.txt', "a") as file:
for currentpage in linkcollector:
file.write("\n%s" %currentpage)

except Exception as e:
#Catch Unknown Exception
with open('C:\Scripts\Logfiles\\'+str(testrun)+'_Logfile.txt', "a") as file:
file.write("\n Unknown Exception: " %str(e))

Every input is appreciated, thanks!

What I have tried:

- python.exe -> Properties -> Run as Administrator

推荐答案

command ='C:\\Python36 \\python.exe C:\\Scripts\\myscript。 py';

pclose(popen(start / B。
command = 'C:\\Python36\\python.exe C:\\Scripts\\myscript.py ';
pclose(popen("start /B ".


command,w));



我试过python.exe和pythonw.exe

脚本正常启动,但大约30分钟后停止。



def getAllLinksOnCurrentPage():

elems = driver.find_elements_by_xpath(// a [@href])
元素中的元素


f = elem.get_attribute(href)

如果f中的testur:

如果f不在linkcollector中:

linkcollector.append (f)





试试:

driver.get(testurl)



获取linkcollector中的当前页面:

driver.get(当前页面)

getAllLinksOnCurrentPage()





#Write all URL s到日志文件

打开('C:\Scripts\Logfiles\\Logfile.txt',a)作为文件:

for currentpage in linkcollector:

file.write(\ n%s%currentpage)



除了例外e:

#Catch未知异常

打开('C:\Scripts\Logfiles\\'+ str(testrun)+'_ Logfile.txt',a)作为文件:

file.write(\ n未知异常:%str(e))



每个输入都表示赞赏,谢谢!



我尝试过:



- python.exe - > ;属性 - >以管理员身份运行
command, "w"));

I tried both python.exe and pythonw.exe
The script is starting normally, but stops after about 30 minutes.

def getAllLinksOnCurrentPage():
elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
f = elem.get_attribute("href")
if testurl in f:
if f not in linkcollector:
linkcollector.append(f)


try:
driver.get(testurl)

for currentpage in linkcollector:
driver.get(currentpage)
getAllLinksOnCurrentPage()


#Write all URLs to Logfile
with open('C:\Scripts\Logfiles\\Logfile.txt', "a") as file:
for currentpage in linkcollector:
file.write("\n%s" %currentpage)

except Exception as e:
#Catch Unknown Exception
with open('C:\Scripts\Logfiles\\'+str(testrun)+'_Logfile.txt', "a") as file:
file.write("\n Unknown Exception: " %str(e))

Every input is appreciated, thanks!

What I have tried:

- python.exe -> Properties -> Run as Administrator


IIS将终止并回收闲置时间过长的进程。这包括你的IIS进程启动的任何子进程。



你应该做的是编写你的Python进程,不是因为网站代码而是作为Windows启动服务。该服务独立于网站代码运行,不受IIS政策和程序的约束。
IIS will kill off and recycle a process that is sitting idle for too long. That includes any child processes that your IIS process launched.

What you should have done is write your Python process not as something kicked off by the website code but as a Windows Service. The service runs independently of the website code and isn't subject to IIS policies and procedures.


这篇关于IIS启动的Python进程在一段时间后停止运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆