如何将time.sleep()作为线程重新运行相同的函数?

2024-09-28 17:19:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,它基本上是Youtube终端搜索器,带有缩略图预览,我决定使用Ueberzug的库进行预览

问题是Ueberzug只在函数运行时预览图像,所以必须使用time.sleep()来预览图像,而循环也不起作用。而且它必须作为线程或进程工作,因为我的程序应该在图像预览时继续它的功能。作为线程运行Ueberzug并没有问题,它工作得很好,但我必须终止该线程,并再次作为线程运行相同的函数

我尝试了几种重新运行线程的方法,但都没有用

下面是必须作为线程运行并重新运行的函数

def ueberzug_image_preview(index_of_image):
        with ueberzug.Canvas() as c:
            paths = ['/path/to/image/{}'.format(index_of_image)]
            demo = c.create_placement('demo', x=0, y=0, scaler=ueberzug.ScalerOption.COVER.value)
            demo.path = paths[0]
            demo.visibility = ueberzug.Visibility.VISIBLE
            time.sleep(600)

#Here is where I must put that in.

def main(stdscr):
    curses.curs_set(0)
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)

    current_row_idx = 0

    print_menu(stdscr, current_row_idx)
    #I need to run that function as thread here first with current_row_idx argument
    while 1:
        key = stdscr.getch()
        stdscr.clear()

        if key == curses.KEY_UP and current_row_idx > 0:
            current_row_idx -= 1
            #I need to stop that thread here and re-run with changed current_row_idx argument.

        elif key == curses.KEY_DOWN and current_row_idx != len(menu)-1:
            current_row_idx += 1
            #I need to stop that thread here and re-run with changed current_row_idx argument.

        elif key == curses.KEY_ENTER or key in [10,13]:
             system("mpv %s > /dev/null" % (urls[current_row_idx]))

        else:
            print_menu(stdscr, current_row_idx)
        print_menu(stdscr, current_row_idx)
        stdscr.refresh()

curses.wrapper(main)

我需要将该函数作为线程放置,并在当前行idx更改时作为线程或进程重新运行该函数


Tags: tokey函数imagethatdemowithcurrent