如何使用time.sleep()作为线程重新运行相同的功能?

时间:2019-07-03 12:30:39

标签: python python-3.x multithreading python-multiprocessing

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

问题在于Ueberzug仅在函数运行时预览图像,因此必须使用time.sleep()预览图像,而While循环也不起作用。而且它还必须作为线程或进程工作,因为我的程序应在预览图像时继续其功能。将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)

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

0 个答案:

没有答案