
[英]No shell prompt message, just a blinking cursor after starting a Python script as a daemon?
[英]Scheduling Python Script but just blinking
我对 python 比较陌生,我遇到了一些日程安排问题。 我有一个简单的 BeautifulSoup 代码,用于解析美国今天的一些新闻标题。
class Command(BaseCommand):
def handle(self, *args, **options):
html = urlopen('')
soup = BeautifulSoup(html, 'html.parser')
site_json=json.loads(soup.text)
for i in range(len(site_json)):
title = site_json[i]['title']
try:
Job.objects.create(
title=title,)
print('%s added' % (title,))
except Exception as e: print(e)
# print('%s already exists' % (title,))
self.stdout.write( 'job complete' )
我的代码运行良好,但我尝试添加计划以每 5 秒运行一次。 (它只是测试)
import time
import schedule
from os import system
def sch():
class Command(BaseCommand):
def handle(self, *args, **options):
html = urlopen('')
soup = BeautifulSoup(html, 'html.parser')
site_json=json.loads(soup.text)
for i in range(len(site_json)):
title = site_json[i]['title']
try:
Job.objects.create(
title=title,)
print('%s added' % (title,))
except Exception as e: print(e)
# print('%s already exists' % (title,))
self.stdout.write( 'job complete' )
schedule.every(5).seconds.do(sch)
while True:
schedule.run_pending()
time.sleep(1)
为了执行 py 什么都没有发生,只是在 shell 处闪烁。 我没有任何输出\错误日志。 那是因为我不知道该怎么做。 我该如何解决这个问题?
您可以使用 cronTab 来设置执行脚本的时间间隔:
import getpass
from crontab import CronTab
cron = CronTab(user=getpass.getuser())
job = cron.new(command='python3 path/to/my/code.py')
job.second.every(5)
cron.write()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.