2015-11-29 39 views
1

我创建了两个模块,它们中有一些函数。我在模块1中的单个函数中包含了2个函数,而在模块2中包含了导入的模块1,编译器中包含的函数often1好像不会执行。穿线时不执行的函数

MODULE 1

import time,thread 

def class_often(): 
    while 1<2: 
     time.sleep(5) 
     print "Custom funtion not often runs." 

def class_often1(): 
    while 1<2: 
     time.sleep(2) 
     print "Custom funtion often runs." 

def compiler(): 
    class_often() 
    class_often1() 

MODULE2

import time,d,thread 

def often(): 
    while 1<2: 
     time.sleep(2) 
     print "funtion not often runs." 

def often1(): 
    while 1<2: 
     time.sleep(2) 
     print "Function often runs." 

thread.start_new_thread(often,()) 
thread.start_new_thread(often1,()) 
thread.start_new_thread(d.compiler,()) 
+1

为什么你不使用[线程](http://stackoverflow.com/questions/5568555/thread-vs-threading)模块? – Pynchia

回答

2

你在一个线程启动编译器,但它调用class_often哪些块,因为它是一个无限循环,使第二函数不能被调用:

def compiler(): 
    class_often() # blocks 
    class_often1() 

您需要thread.start_new_threadd.complier也即:

def class_often(): 
    while True: 
     time.sleep(5) 
     print("Custom funtion not often runs.") 


def class_often1(): 
    while True: 
     time.sleep(2) 
     print("Custom funtion often runs.") 


def compiler(): 
    thread.start_new_thread(class_often,()) 
    class_often1() 

哪些改变会给你的输出后,如:

funtion not often runs. 
Custom funtion often runs. 
Function often runs. 
Custom funtion not often runs. 
funtion not often runs. 
Custom funtion often runs. 
Function often runs. 
funtion not often runs. 
Custom funtion often runs. 
Function often runs. 
funtion not often runs. 
Custom funtion often runs. 
Function often runs. 
........................... 

threading lib还建议在thread库。