change the interval of servertick from 500ms to 20ms. Do not await the send operation

This commit is contained in:
zombieyang
2024-05-09 20:35:11 +08:00
parent e89ddd7a73
commit 0200fce80b
5 changed files with 33 additions and 21 deletions
+11 -5
View File
@@ -9,15 +9,19 @@ def define_comfyui_nodes(sdppp):
return 'Photoshop is not connected'
return True
def call_async_func_in_sdppp_thread(fn):
def call_async_func_in_sdppp_thread(fn, dontwait = False):
handle = {
'done': False,
'result': None
}
sdppp.onNextTick(fn, handle)
while not handle['done']:
pass
return handle['result']
if not dontwait:
while not handle['done']:
pass
return handle['result']
else:
return None
class GetImageFromPhotoshopLayerNode:
@@ -35,6 +39,7 @@ def define_comfyui_nodes(sdppp):
bounds_id = photoshopInstance.layer_name_to_id(use_layer_bounds, id)
async def do_get_history():
return await photoshopInstance.check_layer_bounds_combo_changed(id, bounds_id)
start = time.time()
is_changed, history_state_id = call_async_func_in_sdppp_thread(do_get_history)
if is_changed and history_state_id is None:
return np.random.rand()
@@ -72,6 +77,7 @@ def define_comfyui_nodes(sdppp):
async def do_get_image():
return await photoshopInstance.get_image(layer_id=id, bounds_id=bounds_id)
start = time.time()
image_id, layer_opacity = call_async_func_in_sdppp_thread(do_get_image)
loadImage = LoadImage()
@@ -112,7 +118,7 @@ def define_comfyui_nodes(sdppp):
layer_id = photoshopInstance.layer_name_to_id(layer)
async def do_send():
return await photoshopInstance.send_images(image_ids=ret, layer_id=layer_id)
call_async_func_in_sdppp_thread(do_send)
call_async_func_in_sdppp_thread(do_send, True)
return (None,)
+1 -1
View File
@@ -66,7 +66,7 @@ class SDPPP:
item = self.onNextTickQueue.pop(0)
item[1]['result'] = await item[0]()
item[1]['done'] = True
await asyncio.sleep(0.5)
await asyncio.sleep(0.02)
self.loop.create_task(selfEventLoop())
# only emit by photoshop instance
+10 -8
View File
@@ -252,7 +252,9 @@ class ComfyConnection {
});
socket.on('get_image', async (data, callback) => {
try {
const startTime = Date.now();
const result = await (0,_events_get_image__WEBPACK_IMPORTED_MODULE_5__["default"])(this.comfyURL, data);
console.log('get_image cost', Date.now() - startTime, 'ms');
callback(result);
} catch (e) {
console.error(e);
@@ -469,7 +471,7 @@ async function getImage(comfyURL, params) {
const desireBounds = getDesiredBounds(boundsLayerID);
const pixelDataFromAPI = await getPixelsData(layer, desireBounds);
const pixelDataForReturn = padAndTrimLayerDataToDesireBounds(layer, pixelDataFromAPI, desireBounds);
// console.log('getPixels', Date.now() - startTime, 'ms');
console.log('getPixels', Date.now() - startTime, 'ms');
// log desire size
const image = await new Promise((resolve, reject) => {
new (_library_jimp_min__WEBPACK_IMPORTED_MODULE_2___default())({
@@ -480,27 +482,27 @@ async function getImage(comfyURL, params) {
err ? reject(err) : resolve(image);
});
});
console.log('new Jimp', Date.now() - startTime, 'ms');
image.quality(100);
console.log('quality', Date.now() - startTime, 'ms');
const file = await image.getBufferAsync((_library_jimp_min__WEBPACK_IMPORTED_MODULE_2___default().MIME_PNG));
// console.log('create pngfile', Date.now() - startTime, 'ms');
console.log('create pngfile', Date.now() - startTime, 'ms');
const fd = new FormData();
const PhotoshopBlob = new Blob([file], {
type: "image/png"
});
fd.append('image', PhotoshopBlob, "PhotoshopBlob.png");
fd.append('overwrite', "true");
// console.log('start upload', Date.now() - startTime, 'ms');
console.log('start upload', Date.now() - startTime, 'ms');
const promise = fetch(comfyURL + '/upload/image', {
method: 'POST',
body: fd
}).then(res => {
if (res.status == 200) return res.json();else throw new Error('HTTP ' + res.status);
});
// console.log('finish upload', Date.now() - startTime, 'ms')
console.log('finish upload', Date.now() - startTime, 'ms');
const result = await promise;
// console.log('upload resulted', Date.now() - startTime, 'ms')
console.log('upload resulted', Date.now() - startTime, 'ms');
if (result.error) throw new Error(result.error);
if (!result.name) throw new Error('No upload_name');
uploadName = result.name;
@@ -514,7 +516,7 @@ async function getImage(comfyURL, params) {
activeLayers[i].selected = true;
}
}
if (hostControl && suspensionID) await hostControl.resumeHistory(suspensionID);
if (hostControl && suspensionID) hostControl.resumeHistory(suspensionID);
}
}, {
commandName: "get content of layer " + layerID
+2
View File
@@ -92,7 +92,9 @@ class ComfyConnection {
socket.on('get_image', async (data, callback) => {
try {
const startTime = Date.now();
const result = await getImage(this.comfyURL, data)
console.log('get_image cost', Date.now() - startTime, 'ms');
callback(result)
} catch (e) { console.error(e); callback({ error: e.message }) }
})
+9 -7
View File
@@ -142,7 +142,7 @@ export default async function getImage(comfyURL, params) {
const desireBounds = getDesiredBounds(boundsLayerID);
const pixelDataFromAPI = await getPixelsData(layer, desireBounds)
const pixelDataForReturn = padAndTrimLayerDataToDesireBounds(layer, pixelDataFromAPI, desireBounds)
// console.log('getPixels', Date.now() - startTime, 'ms');
console.log('getPixels', Date.now() - startTime, 'ms');
// log desire size
const image = await new Promise((resolve, reject) => {
new Jimp({
@@ -153,15 +153,17 @@ export default async function getImage(comfyURL, params) {
err ? reject(err) : resolve(image);
})
})
console.log('new Jimp', Date.now() - startTime, 'ms');
image.quality(100);
console.log('quality', Date.now() - startTime, 'ms');
const file = await image.getBufferAsync(Jimp.MIME_PNG);
// console.log('create pngfile', Date.now() - startTime, 'ms');
console.log('create pngfile', Date.now() - startTime, 'ms');
const fd = new FormData();
const PhotoshopBlob = new Blob([file], { type: "image/png" });
fd.append('image', PhotoshopBlob, "PhotoshopBlob.png");
fd.append('image', PhotoshopBlob, "PhotoshopBlob.png") ;
fd.append('overwrite', "true");
// console.log('start upload', Date.now() - startTime, 'ms');
console.log('start upload', Date.now() - startTime, 'ms');
const promise = fetch(comfyURL + '/upload/image', {
method: 'POST',
body: fd,
@@ -171,9 +173,9 @@ export default async function getImage(comfyURL, params) {
else
throw new Error('HTTP ' + res.status)
})
// console.log('finish upload', Date.now() - startTime, 'ms')
console.log('finish upload', Date.now() - startTime, 'ms')
const result = await promise
// console.log('upload resulted', Date.now() - startTime, 'ms')
console.log('upload resulted', Date.now() - startTime, 'ms')
if (result.error) throw new Error(result.error);
if (!result.name) throw new Error('No upload_name')
@@ -190,7 +192,7 @@ export default async function getImage(comfyURL, params) {
activeLayers[i].selected = true;
}
}
if (hostControl && suspensionID) await hostControl.resumeHistory(suspensionID);
if (hostControl && suspensionID) hostControl.resumeHistory(suspensionID);
}
}, { commandName: "get content of layer " + layerID })