在2个函数结束后,线程化2个函数并继续其余的代码

时间:2017-10-19 14:14:29

标签: python multithreading multiprocessing

我想同时运行2个功能。然后等到这两个函数结束,它就可以开始处理剩下的代码了。我尝试使用thread模块但它只是继续运行而不等待那两个函数完成。我的示例代码如下:

import os, sys
import threading
from threading import Thread

class Example():
    def __init__(self):
        self.method_1()

    def method_1(self):
        for i in range(3):
            print ('new')

            def run(self):
                threading.Thread(target = function_a, args=(self,)).start()
                threading.Thread(target = function_b, args=(self,)).start()

            def function_a(self):
                for i in range(10):
                    print (1)

            def function_b(self):
                for i in range(10):
                    print (2)

            run(self)


Example()

如果执行上述代码,print ("new")内的method_1即会在function_afunction_b结束前立即打印出来for i in range(3) 1}}。但是,我想要的是new只会在function_afunction_b完成打印12时打印出来。

也就是说,代码应停在threading并等待function_afunction_b完成,以便它可以继续处理{{1}中的下一个i 1}}。

如果有人知道如何解决这个问题,请告诉我。理解!!

2 个答案:

答案 0 :(得分:1)

正如在另一个答案中已经指出的那样,你需要加入线程。这个例子保留了你的结构。我在那里添加了一个睡眠,以便你可以看到它的工作原理(否则缓冲输出可能会搞砸了)。

import os, sys
import threading
from threading import Thread
from time import sleep

class Example():
    def __init__(self):
        self.method_1()

    def method_1(self):
        for i in range(3):
            print ('new')

            def run(self):
                a=threading.Thread(target = function_a, args=(self,))
                b=threading.Thread(target = function_b, args=(self,))
                a.start()
                b.start()
                a.join()
                b.join()

            def function_a(self):
                for i in range(10):
                    print (1)

            def function_b(self):
                for i in range(10):
                    print (2)

            run(self)
            sleep(1)


Example()

答案 1 :(得分:0)

你需要join个线程(连接意味着“等待它完成”)。请参阅docs。根据您的问题,您可能希望使用multiprocessing代替,如评论中所述。我会假设您确实想要进行线程化。

使用提出问题的方法定义一个类是不必要的(更糟糕的是,您在每次迭代时都重新定义runfunction_afunction_b。只需使用一些功能。如果需要,可以稍后将它们转换为方法。

import time
import threading

def function_a():
    for i in range(5):
        print('a', i)
        time.sleep(.1)

def function_b():
    for i in range(5):
        print('b', i)
        time.sleep(.1)

def run():
    thread_a = threading.Thread(target = function_a)
    thread_b = threading.Thread(target = function_b)

    thread_a.start()
    thread_b.start()

    print('threads started')

    return thread_a, thread_b

for i in range(3):
    print('new', i)

    thread_a, thread_b = run()

    print('about to join threads')

    thread_a.join()
    thread_b.join()

    print('threads joined')

sleep就是为了表明这两个线程确实在同一时间运行。示例输出(可能因运行而异,具体取决于具体时间):

new 0
a 0
b 0
threads started
about to join threads
a 1
b 1
a 2
b 2
a 3
b 3
a 4
b 4
threads joined
new 1
a 0
b 0
threads started
about to join threads
a 1
b 1
a 2
b 2
a 3
b 3
a 4
b 4
threads joined
new 2
a 0
b 0
threads started
about to join threads
a 1
b 1
b 2
a 2
b 3
a 3
b 4
a 4
threads joined

编辑:基于类的示例

class Example():
    def function_a(self):
        for i in range(5):
            print('a', i)
            time.sleep(.1)

    def function_b(self):
        for i in range(5):
            print('b', i)
            time.sleep(.1)

    def get_threads(self):
        thread_a = threading.Thread(target = self.function_a)
        thread_b = threading.Thread(target = self.function_b)

        thread_a.start()
        thread_b.start()

        print('threads started')

        return thread_a, thread_b

    def run(self):
        for i in range(3):
            print('new', i)

            thread_a, thread_b = self.get_threads()

            print('about to join threads')

            thread_a.join()
            thread_b.join()

            print('threads joined')

e = Example()
e.run()