Python interface Automation Nine--face object programming two, build test environment, multi-threading and multi-process

Source: Internet
Author: User
Tags instance method

1. Object-Facing programming

1.1 Basic concepts

Instance method: The function defined in the class is an instance method with a self variable inside the function

Class method:

    1. cannot be instantiated, called directly with the class name
    2. Class methods can use class variables, cls.xxx
    3. Instances can use class methods through SELF.XX
    4. Instance methods and instance variables cannot be used inside a class method

      static method:

    5. Common functions defined in a class
    6. Cannot use instance methods, instance variables, class methods, class variables
    7. Does not need to be instantiated and is called directly with the class name

? Property Method:

    1. is an instance method
    2. Cannot have an entry parameter
    3. When using it, direct m.func, use it as a variable, and do not need parentheses in the back.
    4. It is the return value of the Get function

      destructor: Executed when the instance is reclaimed, and the instance is recycled at the end of the program

      Constructor: the "init" class is automatically executed when instantiated

      Private function: "del" begins with two underscores, declares that the method is private, can only be called inside a class, and cannot be called outside of a class

class My:    def __init__(self):#构造函数,类在实例化的时候,会自动执行         print(‘执行构造函数‘)    def __del__(self): #析构函数,在实例被销毁的时候会自动执行          print(‘执行析构函数‘)    def say(self): #入参里面有self的是实例方法,只有通过实例化才可以调用        print(‘我是牛奶‘)        self.__cry()    def __cry(self): #私有函数,只能在类里面调用        print(‘哇哇哇‘)    @classmethod  #类方法,可以实例化调用,通过类名直接调用    def eat(cls):        print("吃饭")    @staticmethod  #静态方法,不需要实例化,直接用类名调用    def run():        pass    @property   #属性方法,是实例方法,使用的时候直接当做变量来用    def red_pag(self):        return 100

1.2 adorners: They are functions that modify the functionality of other functions

Small examples of adorners

import time,requestsdef timer(func):    def war(*args,**kwargs):        start = time.time()        res = func(*args,**kwargs)        end_time = time.time()        print(‘运行的时间是%s‘%(end_time-start))        return res    return war@timerdef down_img(name):    res=requests.get(‘http://www.nnzhp.cn/wp-content/uploads/2018/07/60f2add20f0659e27d26435f28b8472e.png‘)    open(‘a.png‘,‘wb‘).write(res.content)    return name+‘hhh‘@timerdef eat():    time.sleep(5)res = down_img(‘xxx‘)print(res)eat()

1.3 Inheritance

class Ln:  #父类    money=2000    def make_money(self):         print(‘挣钱‘)class Me(Ln)  #子类继承父类    def make_money(self):         print(‘挣更多的钱’)
2. Build a test environment

First time Building

    1. Install dependent software MYSQL,REDIS,TOMCAT,NGINX,JDK, database, middleware, etc.
    2. Get the code from the Svn,git
    3. Compiling (java,c,c#)
    4. Import the underlying data
    5. Modifying a configuration file
    6. Start Project

Daily deployment

    1. Get the latest code
    2. Compile
    3. Execute SQL file (if database structure is changed)
    4. Modifying a configuration file
    5. Restart Project
3. Multi-threading and multi-process

Process: A process is a program.

Thread: A thread is the smallest execution unit within a process. There is at least one thread in a process that can have multiple threads, each of which is independent of each other.

Because of the Gil (Global interpreter lock) mechanism inside Python, it ensures that only one Python thread executes at any time, so the multi-threaded inside of Python, not using multicore CPUs, can only take advantage of a core CPU. In CPU-intensive processes, multithreading does not lead to an increase in efficiency, but may result in a decrease in efficiency due to frequent thread switching, and in the case of IO-intensive processes, the multithreaded process can take advantage of the idle time of IO blocking wait to execute other threads and improve efficiency.

Multithreading for IO-intensive tasks, multi-process for CPU-intensive tasks

import threading,timeall_res=[]def run():   time.sleep(5)   print(‘%s 哈哈哈‘%name)   all_res.append(name)threads=[] #存放所有的子线程for i in range(10) #开10个子线程   t = threading.Thread(target=run,args=(i,))   threads.append(t)   t.start()   #方法一:统计当前线程数量判断所有子线程是否运行完成while threading.active_count()!=1:       pass    #当while=1,说明子线程已经完成,结束循环,程序向下执行#方法二:主线程循环等待所有的子线程结束后再结束for t in threads:    t.join()#等待子进程运行完以后再运行print(all_res)

Daemon Thread: When the main thread ends, the daemon will also end

import threading,timedef run():   time.sleep(8)   print(‘run。。‘)for i in range(10):   t=threading.Thread(target=run)   t.setDaemon(True) #设置子线程为一个守护进程   t.start()  print(‘over‘)#主线程结束后,子线程就结束了,run函数不会执行

Lock

import threadingfrom threading import Locknum = 0Lock = Lock()def run():    global num    lock.acquire() #加锁    num+=1    lock.release()  #解锁    with lock:  #自动加解锁          num+=1for i in range(100):    t=threading.Thread(target=run)    t.start()while threading.active_count()!=1:    passprint(num)

Thread pool

You can control how many threads are allowed to be at the same time, and the part that exceeds is automatically waited. Fixed the uneven distribution of threads running, such as a thread still working hard, and another thread has completed, but not to help the previous thread sharing situation.

import threadpool,pymongo,requestsclient = pymongo.MongoClient(host=‘118.24.3.40‘,port=27017)table=client[‘liken‘][‘qq_group_likun‘]all_table=[i.get(‘qq‘) for i in table.find()]url = ‘http://q4.qlogo.cn/g?b=qq&nk=%s&s=140‘def down_img(qq_num):    res=requests.get(url%qq_num).content    with open(‘%s.jpg‘%qq_num,‘wb‘) as fw:         fw.write(res)pool = threadpool.ThreadPool(200) #定义线程池的大小all_requests=threadpool.makeRequests(down_img,all_qq) #分配数据for i in all_requests:       pool,putRequest(r) #发请求#[pool.putReques(r) for r in all_requests]pool.wait() #等待所有线程运行完成print(‘done!下载完成。‘)

Python interface Automation Nine--face object programming two, build test environment, multi-threading and multi-process

Related Article

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.