Python将类限制为单个实例?

标签 python cherrypy

我有一个cherryPy服务器,除其他外,访问需要访问“单实例资源”。 单实例资源是与另一个设备的串行通信,需要执行一整套操作才能将其释放以供再次访问。

cherryPy 的定义是多线程,我想保持这种状态。 如何通过管道访问该单一资源? 我能想到的最好办法是添加一个类属性“busy”并在执行对单个资源的访问之前检查其状态。

令我困惑的是 commClass.init print 只打印一次。

    # ========= IMPORTS =============
import cherrypy
import simplejson
import os
import time
import random

class commClass:
    busy = False
    instance = 0

    def __init__(self, uid):
        self.uid = uid
        commClass.instance += 1
        print("INIT:", self.uid, "INST:", commClass.instance)

    def singleResource(self, periods):
        for i in range (0, periods):
            print("ID:", self.uid, " WASTE TIME:", i)
            time.sleep(1)

PATH = os.path.abspath(os.path.dirname(__file__))
comm = commClass(random.randrange(0, 1000))

def wasteTime():
    global comm
    while comm.busy:
        print("busy...")
        time.sleep(0.5)

    comm.busy = True
    comm.singleResource(5)
    comm.busy = False


# ============== Main Server Object ======
class threadTest(object):

    def __init__(self):
        pass

    @cherrypy.expose
    @cherrypy.tools.json_out()
    def wasteTime(self):
        wasteTime()
        servResponse = dict()
        return simplejson.dumps(dict(servResponse))

conf = {'/':
    {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': PATH,
        'tools.staticdir.index': 'cherryPyThreadTest.html',
    }
}


if __name__=='__main__':
    print("Server _ON_")
    #print(Labyrinth.ip)
    cherrypy.server.socket_host = '0.0.0.0'
    cherrypy.server.thread_pool = 30
    cherrypy.server.socket_port = 80
    cherrypy.quickstart(threadTest(), '/', conf)

输出符合预期:

INIT: 643 INST: 1
Server _ON_
ID: 643  WASTE TIME: 0
busy...
busy...
ID: 643  WASTE TIME: 1
busy...
busy...
ID: 643  WASTE TIME: 2
busy...
busy...
ID: 643  WASTE TIME: 3
busy...
busy...
ID: 643  WASTE TIME: 4
busy...
busy...
192.168.252.142 - - [18/Dec/2018:11:22:24] "POST /wasteTime HTTP/1.1" 200 4 "http://xxx.xxx.xxx.xxx/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
ID: 643  WASTE TIME: 0
ID: 643  WASTE TIME: 1
ID: 643  WASTE TIME: 2
ID: 643  WASTE TIME: 3
ID: 643  WASTE TIME: 4

最佳答案

您想要的是一把锁,可以防止多个线程同时运行该函数。

import threading

class Comm(object):
    def __init__(self, uid):
        self.lock = threading.Lock()
        self.uid = uid
    def singleResource(self, periods):
        with self.lock:
            for i in range (0, periods):
                print("ID:", self.uid, " WASTE TIME:", i)
                time.sleep(1)

comm = Comm(0)

def wasteTime():
    comm.singleResource(5)

关于Python将类限制为单个实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53837259/

相关文章:

python - 使用 BeautifulSoup 检查列表中的字符串是否存在于 HTML 中

python - 通过删除转义字符格式化从 URL 获取的 json 数据

python - 如何计算列 Pandas 数据框中列表的平均值

python - 带有用于自定义作业的附加线程的 CherryPy

python - 从 url 文本自动生成文件名

python - Artifactory Conda 包返回 MD5 不匹配错误

python - CherryPy 日志记录 : How do I configure and use the global and application level loggers?

python - CherryPy 不尊重所需的日志记录格式

具有客户端身份验证的 Python 嵌入式 Web 服务器

python - Cherrypy 应用程序重定向到 http 而不是 nginx 后面的 https