python中使用join的带类和不带类多线程

python中使用join的带类和不带类多线程,python,multithreading,python-2.7,join,Python,Multithreading,Python 2.7,Join,我正在为我的项目在python多线程环境中练习。我已经开发了(可能是复制的:)一个示例程序,它将使用线程连接方法,所以主线程将使用线程模型等待其他线程完成 import threading import time class MyThread (threading.Thread): def __init__(self, thread_id, name, counter): threading.Thread.__init__(self) self.thr

我正在为我的项目在python多线程环境中练习。我已经开发了(可能是复制的:)一个示例程序,它将使用线程连接方法,所以主线程将使用线程模型等待其他线程完成

import threading
import time


class MyThread (threading.Thread):
    def __init__(self, thread_id, name, counter):
        threading.Thread.__init__(self)
        self.threadID = thread_id
        self.name = name
        self.counter = counter

    def run(self):
        print "Starting " + self.name
        threadLock.acquire()
        print_time(self.name, self.counter, 3)
        # Free lock to release next thread
        threadLock.release()


def print_time(thread_name, delay, counter):
    while counter:
        time.sleep(delay)
        print "%s: %s" % (thread_name, time.ctime(time.time()))
        counter -= 1

threadLock = threading.Lock()

thread1 = MyThread(1, "Thread-1", 1)
thread2 = MyThread(2, "Thread-2", 2)

thread1.start()
thread2.start()

print 'waiting to finish the thread'

thread1.join()
thread2.join()

print "Exiting Main Thread"
上述各项的产出:

Starting Thread-1
waiting to finish the threadStarting Thread-2

Thread-1: Tue Nov 18 16:31:04 2014
Thread-1: Tue Nov 18 16:31:05 2014
Thread-1: Tue Nov 18 16:31:06 2014
Thread-2: Tue Nov 18 16:31:08 2014
Thread-2: Tue Nov 18 16:31:10 2014
Thread-2: Tue Nov 18 16:31:12 2014
Exiting Main Thread

Process finished with exit code 0
当我对连接方法的这些行进行注释时

#thread1.join()
#thread2.join()
然后我得到的输出是

Starting Thread-1
waiting to finish the threadStarting Thread-2

Exiting Main Thread
Thread-1: Tue Nov 18 16:32:31 2014
Thread-1: Tue Nov 18 16:32:32 2014
Thread-1: Tue Nov 18 16:32:33 2014
Thread-2: Tue Nov 18 16:32:35 2014
Thread-2: Tue Nov 18 16:32:37 2014
Thread-2: Tue Nov 18 16:32:39 2014

Process finished with exit code 0
Hello
Hello
:
Hello
:
Hello
:
Hello
Bye Bye

Process finished with exit code 0
这是预期的 现在我又写了一段代码

import time
import threading


def printer():
    for _ in range(5):
        print 'Hello'
        time.sleep(1)

thread1 = threading.Thread(target=printer())
thread2 = threading.Thread(target=printer())
thread3 = threading.Thread(target=printer())
thread1.start()
thread2.start()
thread3.start()
print 'Bye Bye'
为此,我没有使用线程的
join()
方法,但我得到的输出是

Starting Thread-1
waiting to finish the threadStarting Thread-2

Exiting Main Thread
Thread-1: Tue Nov 18 16:32:31 2014
Thread-1: Tue Nov 18 16:32:32 2014
Thread-1: Tue Nov 18 16:32:33 2014
Thread-2: Tue Nov 18 16:32:35 2014
Thread-2: Tue Nov 18 16:32:37 2014
Thread-2: Tue Nov 18 16:32:39 2014

Process finished with exit code 0
Hello
Hello
:
Hello
:
Hello
:
Hello
Bye Bye

Process finished with exit code 0
通过使用
join()
我也得到了相同的输出

<> P>根据我对线程和连接的理解,当我没有使用<代码>连接()>代码>语句<代码>“再见”/代码>应该打印在中间某处而不是在最后。< /P>
python中的类和简单函数是否有任何行为会发生变化

目标
应该是函数,而不是函数的结果(除非该结果是函数)。例如,
target=printer
。注意没有括号


您所做的有效操作是运行
打印机
三次。然后开始了三条与此无关的线程。然后再见。

目标应该是一个函数,而不是函数的结果(除非该结果是函数)。例如,
target=printer
。注意没有括号


您所做的有效操作是运行
打印机
三次。然后开始了三条与此无关的线程。然后打印拜拜。

从哪里
进程结束,退出代码0
退出主线程
来?退出主线程-我在代码中有打印语句。进程完成….,当程序成功运行时,python代码返回0。从哪里开始
进程完成,退出代码0
退出主线程来?退出主线程-我在代码中有打印语句。进程完成….,当程序运行成功时python代码返回0当我使用括号时函数的结果。。请您详细说明一下好吗?当您编写
printer()
时,您会立即调用该函数。写入
target=printer()
时,调用printer并将target设置为返回值(
None
)。你要做的是给线程对象一个函数,让它在运行时调用。感谢Dunes在我使用括号时对函数结果的澄清。。请您详细说明一下好吗?当您编写
printer()
时,您会立即调用该函数。写入
target=printer()
时,调用printer并将target设置为返回值(
None
)。您要做的是在线程对象运行时为其调用一个函数。感谢Dunes的澄清