python中的多处理卡住了一段时间的True循环

时间:2015-11-01 03:03:29

标签: python multithreading

由于某种原因代码没有到达main函数。我使用cloud9来运行代码,这可能是问题所在。

from multiprocessing import Process, Value
import time

def main():
    print "main function"

def market_price_thread():
    while True:
        market_price()
        time.sleep(5)


def market_price():
    #do something
    print "end"



def start_threads():

    thread = Process(target=market_price_thread())
    thread.start()

    time.sleep(5)


if __name__ == '__main__':
    start_threads()
    main() #does not seem to get to this 

1 个答案:

答案 0 :(得分:1)

您已要求Python拨打market_price_thread

thread = Process(target=market_price_thread())

然后使用它返回的任何值作为target值。因此,在致电Process之前,我们必须等待market_price_thread返回。它返回什么价值,何时返回?

(与Process(target=market_price_thread)比较,调用market_price_thread,而是将该函数传递给Process,以便Process可以叫它。)