美文网首页
多线程编程

多线程编程

作者: 求索_700e | 来源:发表于2019-07-10 08:43 被阅读0次

#coding:utf-8

import threading

import time

event = threading.Event()

def thread_center():

  while(True):

      print("我是thred_center,我在等待函数thread_split来触发我...")

      event.wait()

      print("函数thred_center说:尼玛!我被函数thread_split给触发了...")

      event.clear()

def thread_side():

  while(True):

    print("我是thred_side,我在等待函数thread_split来触发我...")

    event.wait()

    print("函数thred_side说:尼玛!我被函数thread_split给触发了...")

    event.clear()

def thread_split():

  while(True):

      time.sleep(3)

      print("我是thread_split,我开始去触发函数thred_center and thred_side")

      event.set()

def main():

    #url_queue = Queue(maxsize=10)

    thrd_cent = threading.Thread(target=thread_center)

    thrd_side = threading.Thread(target=thread_side)

    thrd_spli = threading.Thread(target=thread_split)

    # 设置线程组

    threads = []

    # 添加到线程组

    threads.append(thrd_cent)

    threads.append(thrd_side)

    threads.append(thrd_spli)

    # 开启线程

    for thread in threads:

        thread.start()

    for t in threads:

        t.join()

    print("主进程结束!")

if __name__ == '__main__':

    main()

=================================================

#coding:utf-8

import threading

import time

from queue import Queue

#from split_img.split import *

event = threading.Event()

def thread_center(data_queue, result_queue):

  while(True):

      print("我是thred_center,我在等待函数thread_split来触发我...")

      event.wait()

      print("函数thred_center说:我被函数thread_split给触发了...")

      print("center:",data_queue.get(1))

      result_queue.put(11)

      if data_queue.empty():

        event.clear()

def thread_side(data_queue, result_queue):

  while(True):

    print("我是thred_side,我在等待函数thread_split来触发我...")

    event.wait()

    print("函数thred_side说:我被函数thread_split给触发了...")

    print("side:",data_queue.get(1))

    result_queue.put(22)

    if data_queue.empty():

        event.clear()

def thread_split(data_queue_cent, data_queue_side):

  while(True):

      time.sleep(1)

      print("我是thread_split,我开始去触发函数thred_center and thred_side")

      data_queue_cent.put(1)

      data_queue_side.put(2)

      event.set()

def thread_draw(cent_result_queue,side_result_queue):

    while(True):

      time.sleep(5)

      print("我是thred_draw,我在等待函数来触发我...")

      event.wait()

      print("函数thred_draw说:我被 3个 函数给触发了...")

      print("draw_cent:",cent_result_queue.get(1))

      print("draw_side:",side_result_queue.get(1))

      if cent_result_queue.empty() or side_result_queue.empty():

            event.clear()

def main():

    cent_data_queue = Queue(maxsize=10)

    side_data_queue = Queue(maxsize=10)

    cent_res_queue = Queue(maxsize=10)

    side_res_queue = Queue(maxsize=10)

    thrd_cent = threading.Thread(target=thread_center,args=(cent_data_queue,cent_res_queue))

    thrd_side = threading.Thread(target=thread_side,args=(side_data_queue,side_res_queue))

    thrd_spli = threading.Thread(target=thread_split,args=(cent_data_queue,side_data_queue))

    thrd_draw = threading.Thread(target=thread_draw,args=(cent_res_queue, side_res_queue))

    # 设置线程组

    threads = []

    # 添加到线程组

    threads.append(thrd_cent)

    threads.append(thrd_side)

    threads.append(thrd_spli)

    threads.append(thrd_draw)

    # 开启线程

    for thread in threads:

        thread.start()

    for t in threads:

        t.join()

    print("主进程结束!")

if __name__ == '__main__':

    main()

相关文章

网友评论

      本文标题:多线程编程

      本文链接:https://www.haomeiwen.com/subject/udtckctx.html