本章内容:
1、手动执行模块里的内容
新建一个文件叫process_Pool.py,然后手动执行
1 #手动执行时,__name__ 等于__main__的,所以会执行测试代码 2 3 import multiprocessing 4 import time,threading 5 6 def thread_run(): 7 print(threading.get_ident()) 8 9 def run(name):10 time.sleep(2)11 print('hello',name)12 t = threading.Thread(target=thread_run,)13 t.start()14 15 print(__name__) #打印当前的文件名等于 __main__16 17 if __name__=="__main__": #手动执行时,成立,所有执行18 for i in range(10):19 p = multiprocessing.Process(target=run,args=('boy%s'%i,))20 p.start()21 p.join()
2、将文件整体导入到另一个模块中使用
新建一个文件modle_poo.py,将process_Pool.py导入执行。由于__name__等于模块名process_Pool,所有if__name__=="__main__" 不成立
1 import sys,os 2 BASE_DIR = os.path.normpath(os.path.join( 3 os.path.abspath(__file__), 4 os.path.pardir 5 )) 6 sys.path.insert(0,BASE_DIR) 7 # print(BASE_DIR) 8 # print(sys.path) 9 10 11 import process_Pool12 13 #执行结果是 process_Pool #等于模块名,所有不执行if__name__=="__main__"
__author__ = "Alex Li"import socketHOST = 'localhost' # The remote hostPORT = 9999 # The same port as used by the servers = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((HOST, PORT))while True: msg = bytes(input(">>:"), encoding="utf8") s.sendall(msg) data = s.recv(1024) # print('Received', data)s.close()