设计模式 单例模式

开机自启

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值