В настоящее время я пытаюсь выполнить некоторые операции, используя обещания в цикле, но у меня возникли большие утечки памяти.
Моя проблема как раз та, которая указана в этой статье , но, в отличие от автора, я пишу в кофе-сценарии (да, с дефисом. Что означает coffeescript 1.12, а не последнийверсия).Таким образом, я не могу использовать ключевое слово «await» (это случайное предположение, так как каждый раз, когда я хочу его использовать, я получаю сообщение об ошибке «await is notfined»).
Это мой оригинальный код(с утечками памяти):
recursiveFunction: (next = _.noop) ->
_data = @getSomeData()
functionWithPromise(_data).then (_enrichedData) =>
@doStuffWithEnrichedData(_enrichedData)
@recursiveFunction()
.catch (_err) =>
@log.error _err.message
@recursiveFunction()
Итак, в соответствии со статьей, на которую я ссылаюсь, мне придется сделать что-то вроде этого:
recursiveFunction: (next = _.noop) ->
_data = @getSomeData()
_enrichedData = await functionWithPromise(_data)
@recursiveFunction()
Но опять же, я застрял, потому что яне может использовать ключевое слово "ожидание".Каков будет лучший подход тогда?
РЕДАКТИРОВАТЬ:
Вот мой настоящий оригинальный код.То, чего я пытаюсь добиться, - это приложение для распознавания лиц.Эта функция находится в lib, и я использую переменную «Service» для предоставления переменных между библиотеками.Чтобы получить кадр с веб-камеры, я использую opencv4nodejs.
faceapi = require('face-api.js')
tfjs = require('@tensorflow/tfjs-node')
(...)
# Analyse the new frame
analyseFrame: (next = _.noop) ->
# Skip if not capturing
return unless Service.isCapturing
# get frame
_frame = Service.videoCapture.getFrame()
# get frame date, and
@currentFrameTime = Date.now()
# clear old faces in history
@refreshFaceHistory(@currentFrameTime)
#convert frame to a tensor
try
_data = new Uint8Array(_frame.cvtColor(cv.COLOR_BGR2RGB).getData().buffer)
_tensorFrame = tfjs.tensor3d(_data, [_frame.rows, _frame.cols, 3])
catch _err
@log.error "Error instantiating tensor !!!"
@log.error _err.message
# find faces on frames
faceapi.detectAllFaces(_tensorFrame, @faceDetectionOptions).then (_detectedFaces) =>
@log.debug _detectedFaces
# fill face history with detceted faces
_detectedFaces = @fillFacesHistory(_detectedFaces)
# draw boxes on image
Service.videoCapture.drawFaceBoxes(_frame, _detectedFaces)
# Get partial time
Service.frameDuration = Date.now() - @currentFrameTime
# write latency on image
Service.videoCapture.writeLatency(_frame, Service.frameDuration)
# show image
Service.faceRecoUtils.showImage(_frame)
# Call next
_delayNextFrame = Math.max(0, 1000/@options.fps - Service.frameDuration)
setTimeout =>
# console.log "Next frame : #{_delayNextFrame}ms - TOTAL : #{_frameDuration}ms"
@analyseFrame()
, (_delayNextFrame)