#创建进程的开销远大于开启线程的开销
from threading import Thread
from multiprocessing import Process
import time
def task(name):
print('代双华吃%s
'%name)
time.sleep(3)
print('代双华吃%s'%name)
if __name__ =="__main__":
# p = Process(target=task, args=('鸟屎', ))
# p.start()
t = Thread(target=task, args=('鸟屎', ))
t.start()
print('主')
# 同一进程内的所有线程共享此进程内的地址空间
from threading import Thread
from multiprocessing import Process
n = 100
def task():
global n
n = 0
if __name__ =="__main__":
p = Process(target=task)
p.start()
p.join()
# t = Thread(target=task)
# t.start()
# t.join()
print('主', n)
# 瞅PID
# 查看进程pid
from multiprocessing import Process, current_process #current_process只可以查看pid不可以查看ppid(父进程的pid)
import os
def task():
print('子进程的PID是%s,父进程的PID是%s'%(os.getpid(), os.getppid()))
if __name__ =="__main__":
p = Process(target=task)
p.start()
print('主', os.getpid())
# 查看线程pid(为了方便理解暂称子线程)
from threading import Thread, current_thread #current_Thread可以查看pid同os模块的os.getpid()
import os
def task():
print('子线程的PID是%s' % (os.getpid()))
if __name__ =="__main__":
t = Thread(target=task)
t.start()
t.join()
print('主', os.getpid())