background function in Python

前端 未结 3 1148
名媛妹妹
名媛妹妹 2020-11-27 03:15

I\'ve got a Python script that sometimes displays images to the user. The images can, at times, be quite large, and they are reused often. Displaying them is not critical, b

3条回答
  •  长情又很酷
    2020-11-27 03:58

    Do something like this:

    def function_that_downloads(my_args):
        # do some long download here
    

    then inline, do something like this:

    import threading
    def my_inline_function(some_args):
        # do some stuff
        download_thread = threading.Thread(target=function_that_downloads, name="Downloader", args=some_args)
        download_thread.start()
        # continue doing stuff
    

    You may want to check if the thread has finished before going on to other things by calling download_thread.isAlive()

提交回复
热议问题