• 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)


  • 相关阅读:
    05_面向对象基础篇_02-构造方法、匿名对象、对象比较、this关键字
    Android Studio 生成 注入的插件
    Android 手机端自动化测试框架
    性能测试该怎么做
    移动端自动化openatx开源项目介绍,pytest并发测试框架结合
    Appium 并发多进程基于 Pytest框架
    Appium 并发测试基于unitest
    Appium 使用小结
    Pandas 命令整理
    Locust 测试结果通过Matplotlib生成趋势图
  • 原文地址:https://www.cnblogs.com/interdrp/p/15899215.html
Copyright © 2020-2023  润新知