最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

避免使用promises中的内存泄漏并在咖啡脚本中循环(无需等待)

IT培训 admin 1浏览 0评论

避免使用promises中的内存泄漏并在咖啡脚本中循环(无需等待)

我目前正在尝试使用循环中的promises执行一些操作,但最终导致了巨大的内存泄漏。

我的问题正是在this article中指出的问题,但与作者相反,我正在写咖啡脚本(是的,用连字符。这意味着coffeescript 1.12而不是最新版本)。因此,我无法使用“等待”关键字(这是一个随意的猜测,因为每次我想使用它,我得到“等待未定义”错误)。

这是我的原始代码(内存泄漏):

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”变量来公开libs之间的变量。为了从网络摄像头获取帧,我使用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)
回答如下:

解决方案是将发送的张量副本处理为detectFaces。

faceapi.detectAllFaces(_tensorFrame, @faceDetectionOptions).then (_detectedFaces) =>
    (...)
    _tensorFrame.dispose()
    (...)

避免使用promises中的内存泄漏并在咖啡脚本中循环(无需等待)

我目前正在尝试使用循环中的promises执行一些操作,但最终导致了巨大的内存泄漏。

我的问题正是在this article中指出的问题,但与作者相反,我正在写咖啡脚本(是的,用连字符。这意味着coffeescript 1.12而不是最新版本)。因此,我无法使用“等待”关键字(这是一个随意的猜测,因为每次我想使用它,我得到“等待未定义”错误)。

这是我的原始代码(内存泄漏):

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”变量来公开libs之间的变量。为了从网络摄像头获取帧,我使用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)
回答如下:

解决方案是将发送的张量副本处理为detectFaces。

faceapi.detectAllFaces(_tensorFrame, @faceDetectionOptions).then (_detectedFaces) =>
    (...)
    _tensorFrame.dispose()
    (...)
发布评论

评论列表 (0)

  1. 暂无评论