Python's system programming--process

Source: Internet
Author: User

1. Debug (PDB)

Code:

 [[email protected] Gaoji]#   vim test2.py  1 #  !/usr/local/bin/python3  2 #  -*-coding:utf-8-*- 3 4 def   Getaverage (A, B):  5 result = a + b  6 print  ("  result=%d  " %result)  7 return   result  8 9 A = ten B = 2 XX C = a + b  ret = Getaverage (A, b)  print  (ret) 
[[email protected] Gaoji]#python3-m pdb test2.py>/home/weixin/gaoji/test2.py (4) <module>()-defGetaverage (A, b):(Pdb) L1#!/usr/local/bin/python32#-*-coding:utf-8-*-3 4defGetaverage (A, b):5 result = a +b6Print("result=%d"%result)7returnresult8 9 A = ten B = a + c = a +B (Pdb)

############################### #进程 ########################

Multi-tasking understanding:

######### #父进程与子进程的先后顺序理解 ################

[[email protected] process]#Vim 01-process. PY1#!/usr/local/bin/python32#-*-coding:utf-8-*-3 4ImportOS5Import Time6 ret =os.fork ()7 8ifRET = =0:9Print('---child process 1---') Ten Time.sleep (5) 11Print('---child process 2---') 12Else: 13Print('---parent process---') Time.sleep (3) 15 16Print('---over---')

Execution Result:

Summary: The value of RET has two, one is equal to 0, one is greater than 0; White point is executed two times.

################ #全局变量在多进程中不共享 ##############

[[email protected] process]#Vim 02-process. PY1#!/usr/local/bin/python32#-*-coding:utf-8-*-3 4 5ImportOs-6Import Time7 G_sum = 8 9 pid =os.fork ()10ifPID = =0:11Print('---process-01---') G_sum + = 1 13Print('---process-01--%d'%g_sum)14Else: Time.sleep (3) # # #为了保证让子进程优先运行16Print('---process-02---') 17Print('---process-02--%d'%g_sum)18

Execution Result:

[[email protected] process] #  ---process-01------process-01--101---process-02------process-02--100        # # #结果还是100, and did not become 101

Python's system programming--process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.