Python实现多任务(协程、线程、进程)

Python实现多任务

  • 1.多任务-协程
  • 2.多任务-线程
  • 3.多任务-进程

1.多任务-协程

from gevent import monkey
import gevent
import urllib.request

monkey.patch_all()

def my_download(url):
    print('GET:%s' % url)
    resp = urllib.request.urlopen(url)
    data = resp.read()
    print('%d bytes recived from: %s' % (len(data), url))


gevent.joinall([
    gevent.spawn(my_download, 'http://www.baidu.com/'),
    gevent.spawn(my_download, 'http://www.itcast.cn/'),
    gevent.spawn(my_download, 'http://www.itheima.com')
])

2.多任务-线程

"""
线程:多任务版UDP聊天器
"""
import socket
import threading


def send_msg(udp_socket):
    """获取键盘数据并发送给对方"""
    while True:
        msg = input("\n请输入要发送的数据:")
        dest_ip = input("\n请输入对方的ip地址:")
        dest_port = int(input("\n请输入对方的port:"))
        udp_socket.sendto(msg.encode("utf-8"), (dest_ip, dest_port))


def recv_msg(udp_scoket):
    """接受数据并显示"""
    while True:
        recv_msg = udp_scoket.recvfrom(1024)
        recv_ip = recv_msg[1]
        recv_msg = recv_msg[0].decode('utf-8')
        print(">>>%s: %s" % (str(recv_ip), recv_msg))


def main():
    udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udp_socket.bind(("", 7890))
    t = threading.Thread(target=recv_msg, args=(udp_socket,))
    t.start()
    send_msg(udp_socket)


if __name__ == "__main__":
    main()

3.多任务-进程

import multiprocessing
import os
import time
import random


def copy_file(queue, file_name,source_folder_name,  dest_folder_name):
    """copy文件到指定的路径"""
    f_read = open(source_folder_name + "/" + file_name, "rb")
    f_write = open(dest_folder_name + "/" + file_name, "wb")
    while True:
        time.sleep(random.random())
        content = f_read.read(1024)
        if content:
            f_write.write(content)
        else:
            break
    f_read.close()
    f_write.close()

    # 发送已经拷贝完毕的文件名字
    queue.put(file_name)


def main():
    # 获取要复制的文件夹
    source_folder_name = input("请输入要复制文件夹名字:")

    # 整理目标文件夹
    dest_folder_name = source_folder_name + "[副本]"

    # 创建目标文件夹
    try:
        os.mkdir(dest_folder_name)
    except:
        pass  # 如果文件夹已经存在,那么创建会失败

    # 获取这个文件夹中所有的普通文件名
    file_names = os.listdir(source_folder_name)

    # 创建Queue
    queue = multiprocessing.Manager().Queue()

    # 创建进程池
    pool = multiprocessing.Pool(3)

    for file_name in file_names:
        # 向进程池中添加任务
        pool.apply_async(copy_file, args=(queue, file_name, source_folder_name, dest_folder_name))

    # 主进程显示进度
    pool.close()

    all_file_num = len(file_names)
    while True:
        file_name = queue.get()
        if file_name in file_names:
            file_names.remove(file_name)

        copy_rate = (all_file_num-len(file_names))*100/all_file_num
        print("\r%.2f...(%s)" % (copy_rate, file_name) + " "*50, end="")
        if copy_rate >= 100:
            break
    print()


if __name__ == "__main__":
    main()

你可能感兴趣的:(python,python,queue,多进程,多线程)