print('Process (%s) starts...' % os.getpid()) # Only works on Unix / Linux / Mac: pid = os.fork() if pid == 0: print('I am a child process (%s) and my parent is (%s).' % (os.getpid(), os.getppid())) else: print('I (%s) just created a child process (%s).' % (os.getpid(), pid))
运行结果如下:
1 2 3
Process (56351) starts... I (56351) just created a child process (56352). I am a child process (56352) and my parent is (56351).
from multiprocessing import Process import os import time
# 子进程要执行的代码 defrun_proc(name): print('Run child process %s (%s)' % (name, os.getpid())) time.sleep(2) print('Child process ends')
if __name__ == '__main__': print('Parent process %s.' % os.getpid()) p = Process(target=run_proc, args=('test',)) print('Child process will start.') p.start() p.join() print('Parent process end.')
执行结果如下:
1 2 3 4 5
Parent process 56782. Child process will start. Run child process test (56783) Child process ends Parent process end.
if __name__=='__main__': print('Parent process %s.' % os.getpid()) p = Pool(4) for i in range(5): p.apply_async(long_time_task, args=(i,)) print('Waiting for all subprocesses done...') p.close() p.join() print('All subprocesses done.')
执行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
Parent process 57316. Waiting for all subprocesses done... Run task 0 (57317)... Run task 1 (57318)... Run task 2 (57319)... Run task 3 (57320)... Task 0 runs 0.59 seconds. Run task 4 (57317)... Task 4 runs 1.06 seconds. Task 3 runs 1.96 seconds. Task 1 runs 2.02 seconds. Task 2 runs 2.99 seconds. All subprocesses done.
对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close(),不然会出现ValueError: Pool is still running错误。调用close()之后就不能继续添加新的Process了。
Non-authoritative answer: python.org mail exchanger = 50 mail.python.org.
Authoritative answers can be found from: mail.python.org internet address = 82.94.164.166 mail.python.org has AAAA address 2001:888:2000:d::a6 Exit code: 0
from multiprocessing import Process, Queue import os, time, random
# 写数据进程执行的代码: defwrite(q): print('Process to write: %s' % os.getpid()) for value in ['A', 'B', 'C']: print('Put %s to queue...' % value) q.put(value) time.sleep(random.random())
# 读数据进程执行的代码: defread(q): print('Process to read: %s' % os.getpid()) whileTrue: value = q.get(True) print('Get %s from queue.' % value)
Process to write: 50563 Put A to queue... Process to read: 50564 Get A from queue. Put B to queue... Get B from queue. Put C to queue... Get C from queue.
logging.warning('Watch out!') # will print a message to the console logging.info('I told you so') # will not print anything
WARNING:root:Watch out!
打印日志到文件
1 2 3 4 5 6 7 8 9 10
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG) logging.debug('This message should go to the log file') logging.info('So should this') logging.warning('And this, too')
DEBUG:root:This message should go to the log file INFO:root:So should this WARNING:root:And this, too
import logging logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) logging.debug('This message should appear on the console') logging.info('So should this') logging.warning('And this, too')
DEBUG:This message should appear on the console INFO:So should this WARNING:And this, too
日期
1 2 3 4 5
import logging logging.basicConfig(format='%(asctime)s %(message)s') logging.warning('is when this event was logged.')
2019-02-0722:21:20,993is when this event was logged.