19 多程序學習

2022-05-07 02:21:10 字數 1521 閱讀 2675

import multiprocessing

import time

''''''

def func(msg):

print('msg: ', msg)

time.sleep(1)

print('********')

return 'func_return: %s' % msg

if __name__ == '__main__':

'''pool = multiprocessing.pool(processes=4)

results =

for i in range(10):

msg = 'hello world %d' % i

for i in results:

i.wait() # 等待程序函式執行完畢

for i in results:

if i.ready(): # 程序函式是否已經啟動了

if i.successful(): # 程序函式是否執行成功

print(i.get()) # 程序函式返回值

''''''

pool = multiprocessing.pool(processes=4)

results =

for i in range(10):

msg = 'hello world %d' % i

pool.close()

pool.join() # join語句要放在close之後

print(results)

''''''

# map

print('\n--------map------------')

args = [1, 2, 4, 5, 7, 8]

pool = multiprocessing.pool(processes=6)

return_data = pool.map(func, args)

print('阻塞') # 執行完func才執行該句

pool.close()

pool.join() # join語句要放在close之後

print(return_data)

'''# map_async

print('\n--------map_async------------')

pool = multiprocessing.pool(processes=6)

args = [1, 2, 4, 5, 7, 8]

result = pool.map_async(func, args)

print('ready: ', result.ready())

print('不堵塞')

result.wait() # 等待所有程序函式執行完畢

if result.ready(): # 程序函式是否已經啟動了

if result.successful(): # 程序函式是否執行成功

print(result.get()) # 程序函式返回值

Linux多程序學習

1 fork 一次呼叫兩次返回?include include using namespace std int main else 執行結果 i m parent,ppid is 10037,and pid is 10038 i m child,pid is 10038 press to close...

python 多程序學習

講解看 吧 把例子記一下 1.用fork建立程序 import osprint process s start.os.getpid pid os.fork if pid 0 print i am child process s and my parent is s os.getpid os.getp...

python學習 多程序

程序池概念 系統資源分配的最小單位,依賴於程序 特點 檢視程序方法 win 任務管理器 linux top htop ps aux 檢視程序號os.getpid os.getppid 檢視父程序的id 建立 multiprocessing模組process類建立乙個物件 import multipr...