使用gunicorn+flask框架,同时通过apscheduler实现对一款爬虫的定时调用,但是在这其中遇到了一个问题,如下:
1,我利用shell终端,通过gunicorn -c直接运行,然后通过flask构造url调用爬虫,并在终端观察输出信息,可以完美调用爬虫。但是当我运行gunicorn -c之后,关闭终端,则爬虫不能完美运行,通过对爬虫调试,发现在运行一段时间后就被终止。
2,之后我在爬虫中使用os.system('nohup python spider.py &'),并且一直打开终端可以完美运行,但是在关闭终端的情况下,又无法执行到底。
所以请问大家怎么来看一下这个脚本运行中为何自动结束。或者说产生的原因是什么。
Your process has not left the terminal. After the terminal is closed, all child processes associated with the terminal will be terminated without any output.
It is recommended to make spaider.py into a daemon process, out of the control of the terminal.
logging
Thank you for your answers. The real reason has been found. It is because too much print output during the debugging process exceeds the cache and causes the process to end.
Although you execute the process in the background, it is not detached from the terminal that started the process. Therefore, the parent process shell terminal is closed and the child process will also be closed.
Solution 1, you can refer to How to make a Python script run like a service or daemon in Linux, which teaches you how to program a process into a daemon process under Linux. The sample program is quite long, so I’ll explain it at the end.
Solution 2, there are python libraries that can daemonize their own processes, and there are quite a few. For example, python-daemon. Usage examples are as follows:
Additional solution 1 processing: