Inheritance threading.Thread class does not work

后端 未结 2 1866
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 12:27

I\'m new in multithreading, so the answer is probably very simple.

I\'m trying to make two instances of one class and run them parallel. I\'ve read that I can use c

2条回答
  •  情歌与酒
    2020-12-18 12:49

    Python's regular implementation of a thread already knows how to run a task, so unless you're creating a special kind of thread (not a special kind of task) - what you probably want is to use the regular thread:

    def task(_min, _max): # 'min' and 'max' are actual functions!
        time.sleep(_max)
        for _ in range(1000):
            print random.choice(range(_min,_max))
    

    And now create a thread to run the task:

    t1 = threading.Thread(target=task, args=(3, 5,))
    t2 = threading.Thread(target=task, args=(3, 5,))
    
    t1.start()
    t2.start()
    

提交回复
热议问题