# unix 系 下 import os print('进程 id {} 开启...'.format(os.getpid())) pid = os.fork() # 开启新的进程,但仍是先有父进程 if pid == 0: print('我是子进程 id ({}) 被父进程 id ({}) 创建'.format(os.getpid(),os.getppid())) # 是被开启的子进程 else: print('我是父进程 id ({}) 创建了子进程 id ({})'.format(os.getpid(),pid)) # 父进程 用来开启别的进程
进程 id 5697 开启... 我是父进程 id (5697) 创建了子进程 id (7799) 我是子进程 id (7799) 被父进程 id (5697) 创建
import os print("Process (%s) is start..." %os.getpid()) pid = os.fork() print('pid: {}'.format(pid)) if pid == 0: print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid())) else: print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
if __name__=='__main__': print('Parent process %s.' % os.getpid()) p = Process(target=run_proc, args=('test',)) print('Child process will start.') p.start() p.join() print('Child process end.')
Parent process 5697. Child process will start. Run child process test (7870)... Child process end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 使用进程池 from multiprocessing import Pool from multiprocessing import Pool import os,time,random def process_pool_task(name): print('task {} run and its pid ({})'.format(name,os.getpid())) start = time.time() time.sleep(random.random() * 3) end = time.time() print('task {} finished and cost {}'.format(name,(end-start)))
if __name__ == '__main__': print('Parent process %s' %os.getpid()) p = Pool(4) for i in range(5): p.apply_async(process_pool_task,args=(i,)) print('Waiting for all subprocesses done...') p.close() p.join() print('All subprocesses finished!')
from multiprocessing import Process,Queue import os,time,random def write_queue(q): print('write process ({}) start...'.format(os.getpid())) for s in ['A','B','C','D']: print('put value %s into queue...'%s) q.put(s) time.sleep(random.random() * 3) def read_queue(q): print('reading process ({}) start...'.format(os.getpid())) while(True): value = q.get(True) print('Get value %s from queue'%value) if __name__ == '__main__': q = Queue() pw = Process(target=write_queue, args=(q,)) pr = Process(target=read_queue, args=(q,)) pw.start() pr.start() pw.join() pr.terminate()
reading process (8848) start... write process (8847) start... put value A into queue... Get value A from queue put value B into queue... Get value B from queue put value C into queue... Get value C from queue put value D into queue... Get value D from queue
# 重写。。。 from multiprocessing import Process , Queue import os, time , random
def write_queue(q): print('write process ({}) start...'.format(os.getpid())) for w in ['First','Second','Third']: print('write process %s put %s into queue'%(os.getpid(),w)) q.put(w) time.sleep(random.random() * 3) def read_queue(q): print('read process ({}) start...'.format(os.getpid())) while True: value = q.get(True) print('read process {} get {} from queue'.format(os.getpid(),value)) if __name__ == '__main__': q = Queue() pw = Process(target=write_queue, args=(q,)) pr = Process(target=read_queue, args=(q,)) pw.start() pr.start() pw.join() pr.terminate()
from multiprocessing import Process,Queue import os,time,random
def write_queue(q): words = [chr(i) for i in range(97,123)] for word in words: print('Process {} start to write word "{}" into queue...'.format(os.getpid(),word)) q.put(word) time.sleep(random.random() * 3)
def read_queue(q): while True: word = q.get(True) print('Process {} start to read word {} from queue...'.format(os.getpid(),word)) if __name__ == '__main__': q = Queue() pw = Process(target=write_queue,args=(q,)) pr = Process(target=read_queue,args=(q,)) pw.start() pr.start() pw.join() pr.terminate()
线程专栏 threading 高级模块
1 2 3 4 5 6 7 8 9 10 11 12
>>> from threading import Thread,current_thread >>> import time,random >>> def thread_task(): ... print('thread {} start...'.format(current_thread().name)) ... for i in range(5): ... print('thread {} running...{}'.format(current_thread().name,i)) ... print('thread {} finished...'.format(current_thread().name)) ... >>> for n in range(100): ... t = Thread(target=thread_task,name='thread task '+str(n)) ... t.start() ... t.join()
def consumer(): r = '' while True: n = yield r if not n: return print('consumer processing n: {}'.format(n)) r = '200 ok!' def producer(c): #开启生成器 必须 send(None) c.send(None) for n in range(1,6,1): print('producer processing n: {}'.format(n)) r = c.send(n) print('consumer return r:{}'.format(r)) c.close() c = consumer() producer(c)