Simple way to run multiple python threads

前端 未结 1 1652
故里飘歌
故里飘歌 2021-01-27 11:33

I\'m importing multiple python threads from different directories and then want to run them simultaneously.

Here\'s my parent:

import sys
import thread
         


        
1条回答
  •  孤街浪徒
    2021-01-27 12:01

    You also need to provide a tuple with the argument to run the function with. If you have none, pass an empty tuple.

    thread.start_new_thread(test2.main, ())
    

    From the docs of thread.start_new_thread(function, args[, kwargs]) (boldface mine):

    Start a new thread and return its identifier. The thread executes the function function with the argument list args (which must be a tuple). The optional kwargs argument specifies a dictionary of keyword arguments. When the function returns, the thread silently exits. When the function terminates with an unhandled exception, a stack trace is printed and then the thread exits (but other threads continue to run).

    You can also:

    thread = Thread(target = test2.main, args, kwargs)
    

    thread.start() // starts the thread

    thread.join() // wait

    Read more on this approach to creating and working with threads here.

    0 讨论(0)
提交回复
热议问题