python3 并发编程
作者:互联网
多进程
macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % python3 test.py main test 0 test 0 test 1 test 1 test 2 test 2 test 3 test 3 test 4 test 4 test 5 test 5 test 6 test 6 test 7 test 7 test 8 test 8 test 9 test 9 macname@MacdeMacBook-Pro Desktop % cat test.py import multiprocessing from multiprocessing import Pool import time def test1(): for i in range(10): time.sleep(1) print('test', i) def test2(): for i in range(10): time.sleep(1) print('test', i) if __name__ == '__main__': p1 = multiprocessing.Process(target=test1) p2 = multiprocessing.Process(target=test2) p1.start() p2.start() print("main")% macname@MacdeMacBook-Pro Desktop % macname@MacdeMBP Desktop % macname@MacdeMBP Desktop % cat test.py import time from multiprocessing import Process def func(name, sec): print(name, 123) time.sleep(sec) print(name, "123end") if __name__ == '__main__': p1 = Process(target=func, args=("进程1", 1)) p2 = Process(target=func, args=("进程2", 2)) p3 = Process(target=func, args=("进程3", 3)) p1.daemon = True # 必须在start()之前设置守护进程 p1.start() p2.start() p3.start() time.sleep(0.5) print("主进程结束,主进程还得等待其他子进程(非守护进程)结束才行") macname@MacdeMBP Desktop % python3 test.py 进程1 123 进程2 123 进程3 123 主进程结束,主进程还得等待其他子进程(非守护进程)结束才行 进程2 123end 进程3 123end macname@MacdeMBP Desktop %
多线程
如果你的主线程在退出的时候,不用等待那些子线程完成,那就设置这些线程的daemon属性。即,在线程开始(thread.start())之前,调用setDeamon()函数,设定线程的daemon标志。(thread.setDaemon(True))就表示这个线程“不重要”。 如果你想等待子线程完成再退出,那就什么都不用做。,或者显示地调用thread.setDaemon(False),设置daemon的值为false。新的子线程会继承父线程的daemon标志。整个Python会在所有的非守护线程退出后才会结束,即进程中没有非守护线程存在的时候才结束。 macname@MacdeMacBook-Pro Desktop % cat test.py import time import threading def fun(): print("start fun") time.sleep(2) print("end fun") print("main thread") t1 = threading.Thread(target=fun,args=()) #t1.setDaemon(True) t1.start() time.sleep(1) print("main thread end") macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % python3 test.py main thread start fun main thread end end fun macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % cat test.py import time import threading def fun(): print("start fun") time.sleep(2) print("end fun") print("main thread") t1 = threading.Thread(target=fun,args=()) t1.setDaemon(True) t1.start() time.sleep(1) print("main thread end")% macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % python3 test.py main thread start fun main thread end macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop %
协程
# Greenlet对象创建 job = Greenlet(target0, 3) Greenlet.spawn() # 创建一个协程并启动 Greenlet.spawn_later(seconds=3) # 延时启动 # 协程启动 job.start() # 将协程加入循环并启动协程 job.start_later(3) # 延时启动 # 等待任务完成 job.join() # 等待任务完成 job.get() # 获取协程返回的值 # 任务中断和判断任务状态 job.dead() # 判断协程是否死亡 job.kill() # 杀死正在运行的协程并唤醒其他的协程,这个协程将不会再执行,可以 job.ready() # 任务完成返回一个真值 job.successful() # 任务成功完成返回真值,否则抛出错误 # 获取属性 job.loop # 时间循环对象 job.value # 获取返回的值 # 捕捉异常 job.exception # 如果运行有错误,获取它 job.exc_info # 错误的详细信息 # 设置回调函数 job.rawlink(back) # 普通回调,将job对象作为回调函数的参数 job.unlink() # 删除回调函数 # 执行成功的回调函数 job.link_value(back) # 执行失败的回调函数 job.link_exception(back) macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % cat test.py from gevent import monkey monkey.patch_all() # 记住一定放在第一行,这里是打补丁的意思,time模块在使用协程gevent模块的时候,必须打补丁才行,记得放在第一行。 import gevent import time def eat(name): print(f"{name} eat first") time.sleep(3) print(f"{name} eat second") def play(name): print(f"{name} play phone 1") time.sleep(2) print(f"{name} play phone 2") g1 = gevent.spawn(eat, "lily") g2 = gevent.spawn(play, name="lily") g1.join() g2.join()% macname@MacdeMacBook-Pro Desktop % macname@MacdeMacBook-Pro Desktop % python3 test.py lily eat first lily play phone 1 lily play phone 2 lily eat second macname@MacdeMacBook-Pro Desktop %
标签:Pro,编程,print,macname,并发,MacdeMacBook,test,Desktop,python3 来源: https://www.cnblogs.com/sea-stream/p/14071224.html