python中的Process


from multiprocessing import Process
import time
import os

#
# def acb(n):
#     print(n)
#
#
# if __name__ == '__main__':
#     p1 = Process(target=acb, args=("ready", ))
#     p1.start()
#     time.sleep(1)
#     print("end")


# class MP(Process):
#     def __init__(self, this):
#         super().__init__()
#         self.this = this
#         print(self.this)
#
#     def run(self):
#         print("hehe")
#         print(os.getpid())
#         print(self.pid)
#
#     def start(self):
#         super().start()
#         print("game over")
#
#
# if __name__ == '__main__':
#     mp = MP("2b")
#     mp.start()
#     time.sleep(1)
#     print("可以滾了!")

# print(bin(10))
# print(int("0b1010", 2))
# print(oct(10))
# print(int("0b1010", 2))
# print(hex(10))
# print(int("0xa", 16))


# a = 10
# def dd():
#     global a
#     a = 11
#     print(a)
# dd()
# print(a)

# def abc(x, y, m, n):
#     print(m-n)
#     print(x, y)
#
#
# if __name__ == '__main__':
#     p = Process(target=abc, args=(5, 0), kwargs={"n": 100, "m": 998})  # 關鍵字參數必須對應相同的關鍵字名稱
#     p.start()
#     print("睡一會!")
#     time.sleep(10)
#     print("咚咚咚, 結束了")


# me = 998

#
# def sub_process():
#     st_time = time.time()
#     global me # 引入全局變量
#     me = 666
#     print(me)
#     end_tiem = time.time()
#     print(end_tiem-st_time)
#
#
# if __name__ == '__main__':
#     main_p = Process(target=sub_process)  # 定義子進程
#
#     main_p.start()
#     time.sleep(3)   # 趴三秒
#     print(me)  # main進程變量


# def c():
#     print(5)
#     time.sleep(5)
#     print(4)
#     time.sleep(5)
#
#
# if __name__ == '__main__':
#     p = Process(target=c)
#     p.start()
#     time.sleep(6)
#     p.terminate()
#     print("end")


# join 方法, 等待子進程執行結束執行下后續代碼(阻塞狀態)
# def p(m=0):
#     print(m, 111)
# if __name__ == '__main__':
#     # p1 = Process(target=p,)
#     # p1.start()
#     # p1.join()
#     p_lst = []
#     for i in range(8):
#         p1 = Process(target=p, args=(i,))
#         p1.start()
#         p_lst.append(p1)
#         # p1.join() # 等待進程結束后執行下一次循環,效率低
#     for p in p_lst:  # 所有進程異步, 等待結束執行主程序, 阻塞等待所有進程結束后放開
#         p.join()
#
#     print("end")

# 僵屍進程, 子程序還在執行,主程序已經執行完畢, 但是主進程並未完全退出, 依然跟隨子進程
# 孤兒進程, 主進程開啟子進程后異常關閉, 子進程依然繼續執行
# 守護進程,  主進程退出后,子進程會跟隨其同時退出, 不受保護, 主進程退出時不考慮子進程的運行狀態,
# def pp():
#     time.sleep(2)
#     print(1)
# if __name__ == '__main__':
#     p = Process(target=pp,)
#     p.daemon = True  #  設置p為守護進程,
#     p.start()
#     # p.daemon = True  # 沒開始就結束了,要報錯的
#     print(11)

# 非阻塞# 設置阻塞狀態立即執行,不等待
# import socket
# s = socket.socket()
# address = ("192.168.15.102", 8880)
# s.bind(address)
# s.setblocking(False)  # 不等待..,要報錯的
# s.listen(0)
# c, d = s.accept()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM