python 设计模式 单例模式

开机自启

python打包exe开机自动启动的实例(windows)
https://www.jb51.net/article/164217.htm

Python读取ini配置文件的方式
https://www.cnblogs.com/skaarl/p/10274116.html


import win32api
import win32con

class AutoRun():
  def __init__(self):
    name = 'translate' # 要添加的项值名称
    path = 'D:\\python_work\\work\dist\\translate.exe' # 要添加的exe路径
    # 注册表项名
    KeyName = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run'
    # 异常处理
    try:
      key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, KeyName, 0, win32con.KEY_ALL_ACCESS)
      win32api.RegSetValueEx(key, name, 0, win32con.REG_SZ, path)
      win32api.RegCloseKey(key)
    except:
      print('添加失败')
    print('添加成功!')
if __name__=='__main__':
    auto=AutoRun();

单例实现

参考文档

python如何实现单例模式

https://jingyan.baidu.com/article/4b07be3cf2c68148b380f3d5.html

Python中的单例模式的几种实现方式的及优化

https://www.cnblogs.com/huchong/p/8244279.html

python 只运行一个实例(windows 自启动)
https://www.jianshu.com/p/06134ca966de

class Singleton(type):
    """使用元类的metaclass属性实现单例"""
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]
class Monitor(metaclass=Singleton):
    pass


singleton

https://pypi.org/project/tendo/

https://github.com/pycontribs/tendo/blob/master/tendo/singleton.py

> easy_install tendo
> pip install tendo

from tendo import singleton
single = singleton.SingleInstance()


apscheduler

> pip install apscheduler


from apscheduler.scheduler import Scheduler
def run():
    pass
scheduler = Scheduler(standalone=True)
scheduler.add_interval_job(run, seconds=3, max_instances=1)


posted @ 2022-02-16 10:05  锐洋智能  阅读(57)  评论(0编辑  收藏  举报