Python学习笔记24:多进程

#多进程
——进程间通讯(IPC:InterProcessCommunication)
——进程之间无任何共享状态



import multiprocessing
from time import sleep, ctime
class ClockProcess(multiprocessing.Process):
def __init__(self, interval):
super().__init__()
self.interval = interval

def run(self):
while True:
print('The time is %s'%ctime())
sleep(self, interval)

if __name__ == '__main__':
p = ClockProcess(3)
p.start()
while True:
print('sleeping......')
sleep(1)


sleeping......
Process ClockProcess-1:
Traceback (most recent call last):
File "C:\Users\Bruce\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\迅雷下载\demo.py", line 81, in run
sleep(self, interval)
NameError: name 'interval' is not defined
The time is Mon Dec 24 23:38:25 2018
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......
sleeping......

Process finished with exit code -1

 

 

 

from multiprocessing import Process
import os
def info(title):
print(title)
print('module name:', __name__)
#得到父进程的id
print('parent process:', os.getppid())
#得到本身进程的id
print('process id:', os.getpid())

def f(name):
info('function f')
print('hello', name)

if __name__ == '__main__':
info('main line')
p = Process(target = f, args = ('bob',))
p.start()
p.join()

main line
module name: __main__
parent process: 94984
process id: 103092
function f
module name: __mp_main__
parent process: 103092
process id: 21428
hello bob

Process finished with exit code 0

转载于:https://www.cnblogs.com/chickenwrap/p/10171773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值