进程的研究

进程的研究

下面有两个程序都是调用10个子进程进行打印。

第一个
##########################################################################
# thread basics: start 10 copies of a function running in parallel;
# uses time.sleep so that the main thread doesn't die too early--this
# kills all other threads on both Windows and Linux; stdout is shared:
# thread outputs may be intermixed in this version occasionally.
##########################################################################

import thread, time

def counter(myId, count):                    # this function runs in threads
    for i in range(count):
        #time.sleep(1)
        print 'Process ID [%s] => %s' % (myId, i)
if __name__=='__main__':
    for i in range(10):                          # spawn 10 threads
        thread.start_new(counter, (i, 30))        # each thread loops 3 times

    time.sleep(1)
    print 'Main thread exiting.'                 # don't exit too early


从输出结果可以看到这10个子进程是并行运行的。

但是第二个程序的输出结果,却是一直是第一个进程在运行
# OS independent
# Endless loop to print

import thread,time

# functioin
def myprint(pid):
    while 1:
        print "child process [%s]" %(pid)

if __name__=='__main__':
                # Make 10 child threads
    for i in range(10):
        # invoke myprint() in the child threads
        thread.start_new(myprint(i))
    time.sleep(1)

哪位大侠能不能解释一下?
楼上正解,谢谢。

看来小错误会误大事。