У меня есть сервер cherryPy, к которому, помимо прочего, необходим доступ для доступа к «единственному ресурсу».Ресурс с одним экземпляром - это последовательная связь с другим устройством, и необходимо выполнить весь набор операций, прежде чем его можно будет снова освободить для доступа.
cherryPy по определению является многопоточным, и я хотел бы сохранить его таким. Как я могу передать доступ к этому единственному ресурсу? Лучшее, что я мог придумать, - это добавить атрибут класса "занят" и проверить его состояние перед выполнением доступа к единственному ресурсу.
Чтоменя смущает то, что 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