GVKun编程网logo

在Python 3中安排重复事件(python中重复步骤)

16

在本文中,我们将详细介绍在Python3中安排重复事件的各个方面,并为您提供关于python中重复步骤的相关解答,同时,我们也将为您带来关于cron–如何在CentOS6.7中安排重复重启?、impo

在本文中,我们将详细介绍在Python 3中安排重复事件的各个方面,并为您提供关于python中重复步骤的相关解答,同时,我们也将为您带来关于cron – 如何在CentOS 6.7中安排重复重启?、import pyttsx在python 2.7中工作,但不在python3中、property在python2和python3中的区别、python watchdog修改并创建了重复事件的有用知识。

本文目录一览:

在Python 3中安排重复事件(python中重复步骤)

在Python 3中安排重复事件(python中重复步骤)

我正在尝试安排一个重复事件在Python 3中每分钟运行一次。

我看过课堂,sched.scheduler但是我想知道是否还有另一种方法可以做到。我听说有人提到我可以为此使用多个线程,我不介意这样做。

我基本上是在请求一些JSON,然后解析它。它的价值会随着时间而变化。

要使用它,sched.scheduler我必须创建一个循环以请求它安排偶数运行一小时:

scheduler = sched.scheduler(time.time, time.sleep)# Schedule the event. THIS IS UGLY!for i in range(60):    scheduler.enter(3600 * i, 1, query_rate_limit, ())scheduler.run()

还有什么其他方法可以做到这一点?

答案1

小编典典

您可以使用threading.Timer,但是它也可以安排一次性事件,类似于.enter调度程序对象的方法。

将一次性调度程序转换为周期性调度程序的正常模式(使用任何语言)是使每个事件以指定的时间间隔重新进行调度。例如,使用时sched,我不会像您正在使用的那样使用循环,而是像这样:

def periodic(scheduler, interval, action, actionargs=()):    scheduler.enter(interval, 1, periodic,                    (scheduler, interval, action, actionargs))    action(*actionargs)

并通过电话发起整个“永久定期计划”

periodic(scheduler, 3600, query_rate_limit)

或者,我可以使用threading.Timer代替scheduler.enter,但是模式非常相似。

如果您需要更精细的变体(例如,在给定时间或在某些条件下停止定期重新计划),那么添加一些额外的参数就不太难了。

cron – 如何在CentOS 6.7中安排重复重启?

cron – 如何在CentOS 6.7中安排重复重启?

我跑了
crontab -e

并将此行添加到crontab以每天早上1点重启我的机器.

0 1 * * * root /sbin/shutdown -r Now

当crond运行时,我在日志中看到了这一点

Aug 20 01:00:01 stc-logs CROND[30791]: (root) CMD (root /sbin/shutdown -r Now)

但是,系统不会重启.

有任何想法吗?

-Craig

您正在使用/ etc / crontab中具有第6列中的用户ID的条目的语法,但是如果您使用crontab -e,则您正在编辑/ var / spool / crontab中的条目,该条目没有此列,因为它们已按用户分隔.

换句话说,这就是你要放在/ etc / crontab中的内容:

0 1 * * * root /sbin/shutdown -r Now

这是你在做crontab -e时应该输入的内容(假设你是root用户,否则它将不起作用)

0 1 * * * /sbin/shutdown -r Now

换句话说,您现在正在尝试执行命令’root’,据我所知,该命令不存在.

import pyttsx在python 2.7中工作,但不在python3中

import pyttsx在python 2.7中工作,但不在python3中

问题:导入pyttsx时为什么 python3无法找到引擎模块?

细节:

我在Raspbian Wheezy的树莓派上做这个

在python 2.7下,以下工作:

>>> import pyttsx

在python3下,会发生以下情况:

>>> import pyttsx
Traceback (etc...)
 File "<stdin>",line 1,in <module>
 File "/usr/local/lib/python3.2/dist-packages/pyttsx-1.1-py3.2.egg/pyttsx/__init__.py",line 18,in <module>
ImportError: No module named engine

我安装并使用了sudo pip install pyttsx

我已经导入了sys

sys.path包含这个……

>>> print (sys.path) 
['','/usr/local/lib/python3.2/dist-packages/setuptools-5.4.1-py3.2.egg','/usr/local/lib/python3.2/dist-packages/pyttsx-1.1-py3.2.egg','/usr/lib/python3.2','usr/lib/python3.2/plat-linux2','/usr/lib/python3.2/lib-dynload','/usr/local/lib/python3.2/dist-packages','/usr/lib/python3/dist-packages']

ls /usr/local/lib/python3.2/dist-packages导致…

easy-install.pth pyttsx-1.1-py3.2.egg setuptools-5.4.1-py3.2.egg setuptools.pth

unzip -t /usr/local/lib/python3.2/dist-packages/pyttsx-1.1-py3.2.egg显示….

pyttsx/__init__.py  OK
pyttsx/voice.py   OK
pyttsx/engine.py  OK
(etc...)
No errors detected in compressed data of pyttsx-1.1-py3.2.egg

谢谢你的帮助!

解决方法

我相信你在寻找图书馆:

pyttsx3

这个python3兼容版本现在打包在pypi中,对python2和python3都很好用,据我测试过,它没有给出任何错误.

只需使用:

pip install pyttsx3

用法:

import pyttsx3
engine = pyttsx3.init()
engine.say("I am talking Now ");
engine.setProperty('rate',100)  
engine.runAndWait();

property在python2和python3中的区别

property在python2和python3中的区别

  • 问题背景: 源于公司的原来的代码是python2开发的,后来改为python3开发,设计到的property的用法有点不一样
  • 直接上代码

公司原来的python2的代码

class LineItem:

    def __init__(self, description, weight, price):
        self.description = description
        self.__weight = weight
        self.price = price

    @property
    def weight(self):
        return self.__weight

    @weight.setter
    def set_weight(self, value):
        if value > 0:
            self.__weight = value
        else:
            raise ValueError(''weight must be > 0'')

运行代码

In [2]: l = LineItem(''a'', 3, 6)

In [3]: l.weight
Out[3]: 3

In [4]: l.weight = 5

In [5]: l.weight
Out[5]: 5

这个代码在python2下面执行没有问题,但是在python3下面执行,会报错,在执行In [4]: l.weight = 5的时候报错

In [4]: l.weight = 5
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-3c1df6104a5e> in <module>
----> 1 l.weight = 5

AttributeError: can''t set attribute
  • 解决方法

按理说,上面的那种写法不是很规范,无论是在python2还是python3的文档实例里面都不是这么写的,所以为了简便和不出错,我们统一使用下面的这种写法

class LineItem:

    def __init__(self, description, weight, price):
        self.description = description
        self.__weight = weight
        self.price = price

    @property
    def weight(self):
        return self.__weight

    @weight.setter
    def weight(self, value):
        if value > 0:
            self.__weight = value
        else:
            raise ValueError(''weight must be > 0'')

主要区别在于这一行def weight(self, value):

python watchdog修改并创建了重复事件

python watchdog修改并创建了重复事件

在Ubuntu上运行,每次创建文件时,都会得到一个修改后的事件和一个创建的事件。

这是设计使然还是我做错了什么?

我正在使用事件处理程序类 PatternMatchingEventHandler

event_handler = MediaFileHandler(ignore_directories=True) observer = Observer() observer.schedule(event_handler, path=directory, recursive=True) observer.start()

如果这是正确的行为,我可以安全地忽略创建的事件吗?

答案1

小编典典

简短的回答: f = open(... ,''w'')生成FileCreatedEventf.flush()或者f.close()可以生成FileModifiedEvent。是的,创建文件通常会同时生成FileCreatedEventFileModifiedEvents

是否可以安全地忽略FileCreatedEvents取决于您要执行的操作。如果您有兴趣在创建文件时做出反应,则需要处理FileCreatedEvents,并且可能忽略FileModifiedEvents,因为在修改文件时可以生成FileModifiedEvents,而无需生成FileCreatedEvents。

尝试使用规范的看门狗脚本(如下),并且所有内容都应该更加清晰。


长答案:要查看发生了什么,请直接从docs运行规范的看门狗程序:

import sysimport timeimport loggingfrom watchdog.observers import Observerfrom watchdog.events import LoggingEventHandlerif __name__ == "__main__":    logging.basicConfig(level=logging.INFO,                        format=''%(asctime)s - %(message)s'',                        datefmt=''%Y-%m-%d %H:%M:%S'')    path = sys.argv[1] if len(sys.argv) > 1 else ''.''    event_handler = LoggingEventHandler()    observer = Observer()    observer.schedule(event_handler, path, recursive=True)    observer.start()    try:        while True:            time.sleep(1)    except KeyboardInterrupt:        observer.stop()    observer.join()

从终端:

% mkdir ~/tmp% cd ~/tmp% script.py

现在,当您在w模式下打开文件时,在Python解释器中:

In [126]: f = open(''/home/unutbu/tmp/foobar'', ''w'')

终端打印

2014-02-05 16:29:34 - <FileCreatedEvent: src_path=/home/unutbu/tmp/foobar>

当您写入文件时,看门狗不报告任何事件:

In [127]: f.write(''Hi'')

但是当你冲洗时

In [128]: f.flush()

它报告一个FileModifiedEvent:

2014-02-05 16:29:55 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>

如果您将更多内容写入文件:

In [129]: f.write('' there'')

同样,关闭文件时将报告FileModifiedEvent,因为更多的输出将刷新到磁盘:

In [130]: f.close()2014-02-05 16:30:12 - <FileModifiedEvent: src_path=/home/unutbu/tmp/foobar>

我们今天的关于在Python 3中安排重复事件python中重复步骤的分享就到这里,谢谢您的阅读,如果想了解更多关于cron – 如何在CentOS 6.7中安排重复重启?、import pyttsx在python 2.7中工作,但不在python3中、property在python2和python3中的区别、python watchdog修改并创建了重复事件的相关信息,可以在本站进行搜索。

本文标签: