편집 기록

편집 기록
  • 프로필 nowp님의 편집
    날짜2021.03.22

    동시성 관련 문의


    함수1은 0.001초 수행 함수2는 5초 수행 할때 일반 while 문에서 두 함수를 호출 하면 약 5초에 한번씩 수행 됩니다. 함수2번 때문에 그런데 함수1을 0.001초 마다 수행 하고 1초에 한번씩 함수2을 수행 하고자 하는데 좋은 방법이 생각 나지 않습니다. 고수님 들 부탁합니다.

    참고로 함수1은 동영상 플레이, 함수2는 5초 동안 연산처리 할때 영상 끈김 없이 동시에 5초 짜리 연산을 수행 하고자 합니다.

    아래는 비동기로 동시 수행 했는데. 원하는 결과는 아닙니다.

    # 동시 구현은 가능하나 가장 긴 시퀀스에 영향을 받아 동영상 처리가 늦어진다.
    
    import asyncio
    from time import time, sleep
    import dlib, cv2
    import numpy as np
    import sys
    
    global cap
    global scaler
    
    ''' 비동기 호출 '''
    async def async_detector():
        print('modeling')    
        await asyncio.sleep(1.0)    
    
    async def async_play():
        print('play~~~')    
        global scaler    
        scaler = 0.3    
        ret, img = cap.read()    
        img = cv2.resize(img, (int(img.shape[1] * scaler), int(img.shape[0] * scaler)))  # 이미지 축소    
        cv2.imshow('win', img)  # 얼굴 찾기 img를 win 창에 플레이 시킴    
    
        if cv2.waitKey(1) == ord('q'):    
            sys.exit(1)    
        # await asyncio.sleep(1.0)
    
    async def main():
        global cap    
        cap = cv2.VideoCapture('./img/girl.mp4')    
        while(True):    
            begin = time()    
            aws = []    
            aws.append(async_play())    
            aws.append(async_detector())    
            await asyncio.gather(*aws)    
            end = time()    
            print("---------async run time: {0:.3f}sec".format(end - begin))    
    
    if __name__ == "__main__":
        loop = asyncio.get_event_loop()      
        loop.run_until_complete(main())      
        loop.close()   
    
  • 프로필 김종석님의 편집
    날짜2020.10.30

    동시성 관련 문의


    함수1은 0.001초 수행 함수2는 5초 수행 할때 일반 화일 문에서 두 함수를 호출 하면 약 5초에 한번씩 수행 됩니다. 함수2번 때문에 그런데 함수1을 0.001초 마다 수행 하고 1초에 한번씩 함수2을 수행 하고자 하는데 좋은 방법이 생각 나지 않습니다. 고수님 들 부탁합니다..

    참고로 함수1은 동영상 플레이, 함수2는 5초 동안 연산처리 할때 영상 끈김 없이 동시에 5초 짜리 연산을 수행 하고자 합니다.

    아래는 비동기로 동시 수행 했는데. 원하는 결과는 아님니다..

    동시 구현은 가능하나 가장 긴 시퀀스에 영향을 받아 동영상 처리가 늦어진다.

    import asyncio

    from time import time, sleep

    import dlib, cv2

    import numpy as np

    import sys

    global cap

    global scaler

    ''' 비동기 호출 '''

    async def async_detector():

    print('modeling')
    
    await asyncio.sleep(1.0)
    

    async def async_play():

    print('play~~~')
    
    global scaler
    
    scaler = 0.3
    
    ret, img = cap.read()
    
    img = cv2.resize(img, (int(img.shape[1] * scaler), int(img.shape[0] * scaler)))  # 이미지 축소
    
    cv2.imshow('win', img)  # 얼굴 찾기 img를 win 창에 플레이 시킴
    
    
    if cv2.waitKey(1) == ord('q'):
    
        sys.exit(1)
    
    # await asyncio.sleep(1.0)
    

    async def main():

    global cap
    
    cap = cv2.VideoCapture('./img/girl.mp4')
    
    while(True):
    
        begin = time()
    
        aws = []
    
        aws.append(async_play())
    
        aws.append(async_detector())
    
        await asyncio.gather(*aws)
    
        end = time()
    
        print("---------async run time: {0:.3f}sec".format(end - begin))
    

    if name == "main":

    loop = asyncio.get_event_loop()  
    
    loop.run_until_complete(main())  
    
    loop.close()