from multiprocessing import Process,current_process
from time import sleep
def work():
print('子进程开始了')
print('PID:%s' % current_process().pid )
sleep(20)
print('子进程结束了')
def main():
p = Process(target=work)
p.start()
#20s
p.join() #阻塞的
#当前子进程 父进程是谁
if __name__ == '__main__':
main()
'''
管道只能存放和拿取出来pickle数据类型
pickle 进制数据 Python中维持数据类型保存 解析
pickle.loads:解析
pickle.dumps:封装
'''
from multiprocessing import Process,Pipe
import pickle
from time import sleep
import sys
from tl_talk import *
def talk_a(p):
#woman
p.send(pickle.dumps('你好'))
while True:
question = pickle.loads(p.recv())
p.send(pickle.dumps(tl_func(question)))
print(question)
sys.stdout.flush()
sleep(1)
def talk_b(p):
while True:
question = pickle.loads(p.recv())
p.send(pickle.dumps(tl_func(question)))
print(question)
sys.stdout.flush()
sleep(1)
def main():
a,b = Pipe()
Process(target=talk_a,args=(a,)).start()
Process(target=talk_b,args=(b,)).start()
if __name__ == '__main__':
main()
'''
队列 queue
可以把数据放入管道,在多个进程之间进行通信
直接可以存储原始数据
'''
#get 取
#put 存
from multiprocessing import Process,Queue
import sys
def custom(q):
for var in range(10):
print('子进程拿到了:%s' % (q.get()))
sys.stdout.flush()
def main():
q = Queue(10) #队列大小
p = Process(target=custom,args=(q,))
p.start()
#父进程作为生产者
for var in range(10):
q.put(var)
if __name__ == '__main__':
main()
from urllib.request import urlopen,Request
#from urllib2 import urlopen, Request
import json
def tl_func(question):
url = 'http://openapi.tuling123.com/openapi/api/v2'
data = json.dumps({
"perception": {
"inputText": {
"text": "%s" % question
},
},
"userInfo": {
"apiKey": "f0f9ee7ccc824ccaa76a4797b164fbe9",
"userId": "005056C00008"
}
}).encode('utf-8')
headers = {'Content-Type':'application/json'}
req = Request(url=url,data=data,headers=headers)
response = json.loads(urlopen(req).read().decode('utf-8'))
result = response['results']
res = ''
for var in result:
res += '\n'.join(list(var['values'].values()))
else:
return res
return None
def main():
result = tl_func(question)
print(result)
if __name__ == '__main__':
question = '你好'
main()
from urllib.request import urlopen,Request
#from urllib2 import urlopen, Request
import json
def tl_func(question):
url = 'http://openapi.tuling123.com/openapi/api/v2'
data = json.dumps({
"perception": {
"inputText": {
"text": "%s" % question
},
},
"userInfo": {
"apiKey": "f0f9ee7ccc824ccaa76a4797b164fbe9",
"userId": "005056C00008"
}
}).encode('utf-8')
headers = {'Content-Type':'application/json'}
req = Request(url=url,data=data,headers=headers)
response = json.loads(urlopen(req).read().decode('utf-8'))
result = response['results']
res = ''
for var in result:
res += '\n'.join(list(var['values'].values()))
else:
return res
return None
def main():
result = tl_func(question)
print(result)
if __name__ == '__main__':
question = '你好'
main()
'''
如果父进程没有对子进程合理回收
父进程没有在子进程工作结束之后立即回收
PCB控制块 PID 僵尸进程
子进程结束 ,父进程存活
'''
from multiprocessing import Process,current_process
from time import sleep
def work():
print('子进程开始了')
print('PID:%s' % current_process().pid )
print('子进程结束了')
def main():
p = Process(target=work)
p.start() #父进程开启了子进程
#p.join() #回收子进程
#父进程退出了
sleep(10) #退出
#p.join()
if __name__ == '__main__':
main()