-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathimage_chooser_server.py
51 lines (46 loc) · 1.58 KB
/
image_chooser_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from server import PromptServer
from aiohttp import web
import time
class Cancelled(Exception):
pass
class MessageHolder:
stash = {}
messages = {}
cancelled = False
@classmethod
def addMessage(cls, id, message):
if message=='__cancel__':
cls.messages = {}
cls.cancelled = True
elif message=='__start__':
cls.messages = {}
cls.stash = {}
cls.cancelled = False
else:
cls.messages[str(id)] = message
@classmethod
def waitForMessage(cls, id, period = 0.1, asList = False):
sid = str(id)
while not (sid in cls.messages) and not ("-1" in cls.messages):
if cls.cancelled:
cls.cancelled = False
raise Cancelled()
time.sleep(period)
if cls.cancelled:
cls.cancelled = False
raise Cancelled()
message = cls.messages.pop(str(id),None) or cls.messages.pop("-1")
try:
if asList:
return [int(x.strip()) for x in message.split(",")]
else:
return int(message.strip())
except ValueError:
print(f"ERROR IN IMAGE_CHOOSER - failed to parse '${message}' as ${'comma separated list of ints' if asList else 'int'}")
return [1] if asList else 1
routes = PromptServer.instance.routes
@routes.post('/image_chooser_message')
async def make_image_selection(request):
post = await request.post()
MessageHolder.addMessage(post.get("id"), post.get("message"))
return web.json_response({})