python learning notes multithreading, thread lock, daemon thread, multiprocess, thread pool, jsonpath module, faker module

Keywords: PHP Python network Firefox SHA1

1. Multithreading

'''
    Process:
        For the operating system, a task is a Process, such as opening a browser is to start a browser Process.
        Opening a notepad starts a notepad process, opening two notepads starts two Notepad processes, and opening a Word starts the process.
        A Word process. A process is a collection of many resources
        Processes and processes are independent of each other.

        thread
            Threads and threads are independent.
            There is a thread in the process itself, which is called the main thread.
The python GIL global interpreter lock causes python's multithreading to fail to take advantage of multicore cpu, but multiprocesses can. When to use multiprocesses: CPU-intensive tasks: when multiple processes, such as sorting or computing When to use multithreading: IO-intensive tasks: multi-threaded input output (frequent network reads and writes, frequent disk reads and writes) A process is equivalent to a factory, and a thread is equivalent to an employee. If a factory wants to operate, it must have at least one employee (a process has one thread by default, the main thread). '''
import threading
import time
def lajifenlei():
    time.sleep(2)#Simulate the memory code run time of this method, assuming that the code runs 2 s
    print('Dry garbage')
# 1.Without multithreading, it takes time to simulate 10 method calls
start_time = time.time()
for n in range(10):
    lajifenlei()
end_time = time.time()
print('Calling 10 methods without multithreading is time-consuming:%s' % (end_time - start_time))

test result

# 2.Multithread simulation-1
start_time = time.time()
for n in range(10):
    xiaohei = threading.Thread(target=lajifenlei,)
    xiaohei.start()
end_time = time.time()
print('The total time consumed to call 10 methods with multithreading is:%s' % (end_time - start_time))#What counts is the time consumed after the main thread runs.

Test results:

# 2.Multithread simulation-2--Actual time-consuming
start_time = time.time()
for n in range(10):#10 sub-threads enabled
    xiaohei = threading.Thread(target=lajifenlei,)
    xiaohei.start()
while threading.active_count() !=1:#Bus threads: 1 main thread+10 Subthreads, when the active thread has only one main thread left, the other threads are executed.
    pass
end_time = time.time()
print('The total time consumed to call 10 methods with multithreading is:%s' % (end_time - start_time))#What counts is the time consumed after the main thread runs.

Test results:


 

# Parametric multithreading
#1.A parameter
import threading
import time

def lajifenlei(name):
    time.sleep(2)#Simulate the memory code run time of this method, assuming that the code runs 2 s
    print('My name is%s,My job is garbage sorting.'%name)

start_time = time.time()
for n in range(10):#10 sub-threads enabled
    xiaohei = threading.Thread(target=lajifenlei,args=('xiaoming',))
    xiaohei.start()
while threading.active_count() !=1:#Bus threads: 1 main thread+10 Subthreads, when the active thread has only one main thread left, the other threads are executed.
    pass
end_time = time.time()
print('The total time consumed to call 10 methods with multithreading is:%s' % (end_time - start_time))#What counts is the time consumed after the main thread runs.

Test results:

# 2.Multiple parameters
import threading
import time

def lajifenlei(name,sex):
    time.sleep(2)#Simulate the memory code run time of this method, assuming that the code runs 2 s
    print('My name is%s,I am one%s Student, my job is garbage sorting.'%(name,sex))

start_time = time.time()
for n in range(10):#10 sub-threads enabled
    xiaohei = threading.Thread(target=lajifenlei,args=('xiaoming','female'))
    xiaohei.start()
while threading.active_count() !=1:#Bus threads: 1 main thread+10 Subthreads, when the active thread has only one main thread left, the other threads are executed.
    pass
end_time = time.time()
print('The total time consumed to call 10 methods with multithreading is:%s' % (end_time - start_time))#What counts is the time consumed after the main thread runs.

//Test results:

# 2.Multiple parameters--list
import threading
import time

def lajifenlei(name,sex):
    time.sleep(2)#Simulate the memory code run time of this method, assuming that the code runs 2 s
    print('My name is%s,I am one%s Student, my job is garbage sorting.'%(name,sex))

start_time = time.time()
list = [['xiaoming1','male'],['xiaoming2','female'],['xiaoming3','female'],['xiaoming4','female'],['xiaoming5','male']]
for l in list:#10 sub-threads enabled
    xiaohei = threading.Thread(target=lajifenlei,args=(l))
    xiaohei.start()
while threading.active_count() !=1:#Bus threads: 1 main thread+10 Subthreads, when the active thread has only one main thread left, the other threads are executed.
    pass
end_time = time.time()
print('The total time consumed to call 10 methods with multithreading is:%s' % (end_time - start_time))#What counts is the time consumed after the main thread runs.

//Test results:

 

#Since multithreaded or process calls cannot obtain the return value of a method, you can define a global variable if you want the return value of a method, and then put the result in that variable.

import threading
import time

list = []

def lajifenlei(name,sex):
    time.sleep(2)#Simulate the memory code run time of this method, assuming that the code runs 2 s
    list.append('My name is%s,I am one%s Student, my job is garbage sorting.'%(name,sex))

start_time = time.time()
list = [['xiaoming1','male'],['xiaoming2','female'],['xiaoming3','female'],['xiaoming4','female'],['xiaoming5','male']]
for l in list:#10 sub-threads enabled
    xiaohei = threading.Thread(target=lajifenlei,args=(l))
    xiaohei.start()
while threading.active_count() !=1:#Bus threads: 1 main thread+10 Subthreads, when the active thread has only one main thread left, the other threads are executed.
    pass
end_time = time.time()
print('list The value is:%s' % list)
print('The total time consumed to call 10 methods with multithreading is:%s' % (end_time - start_time))#What counts is the time consumed after the main thread runs.

//Test results:

Multithread Download File Case

import requests,hashlib

def down_load_file(url):
    r = requests.get(url)
    m = hashlib.md5(url.encode())
    file_name = m.hexdigest()
    print('Downloading%s'%file_name)
    with open('img/%s.jpg'% file_name,'wb') as fw:
        fw.write(r.content)

url_list = ['https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564282263&di=9d1edb5e67c65051336a4ed2c7c5f56a&imgtype=jpg&er=1&src=http%3A%2F%2Fku.90sjimg.com%2Felement_origin_min_pic%2F18%2F03%2F27%2F1d7b7b1c20e2013963d677003e587421.jpg%21%2Ffwfh%2F804x804%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue','https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1693892888,2363320737&fm=26&gp=0.jpg','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563687763106&di=256bf12a6a322a5d161d9c430605eafa&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F02360ab0bb74f7a353a3d121d948d1e497b54dc61db49-bX40Ok_fw658']
start_time = time.time()
for url in url_list:
    t = threading.Thread(target=down_load_file,args=(url,))
    t.start()#Multithreading

while threading.active_count() !=1:
    pass

end_time = time.time()

print('Multithread time-consuming:',end_time-start_time)

Test results:

2. Thread locks (if everyone modifies the same value at the same time, then the value needs to be locked)

import threading
import time

count = 0
lock = threading.Lock()#Apply for a lock
def lajifenlei():
    global count
    lock.acquire()#Where the lock begins
    count += 1
    lock.release()#Lock End

for n in range(1000):
    th = threading.Thread(target=lajifenlei(),)

print('100 After adding 1 count The value is:%s' % count )

//Test results:
100 After adding 1 count The value is:1000

3. Daemon threads

def lajifenlei():
    time.sleep(2)
    print('Dry garbage')

for i in range(10):
    syy = threading.Thread(target=lajifenlei,)
    syy.setDaemon(True)#Set the sub-thread to a daemon thread. As long as the main process is finished, the daemon process will end immediately, whether it is finished or not.
    syy.start()

print('complete')

//Test results:
//complete

4. Multiprocess

import multiprocessing
import time

def lajifenlei():
    time.sleep(2)
    print('Dry garbage')


if __name__ == '__main__':#The calling process must

    for n in range(10):
        mp = multiprocessing.Process(target=lajifenlei,)
        mp.start()
        print('process%s Started'%mp.pid)

    start_time = time.time()
    print(multiprocessing.active_children())
    while len(multiprocessing.active_children()) !=0:#Waiting for the completion of child process execution
        pass
    end_time = time.time()
    print('Time-consuming:%s'%(end_time-start_time))

Test results:

import multiprocessing
import threading
import time

def sayHello(name):
    time.sleep(20)
    print('%s How do you do!'%name)

def lajifenlei():
    print('Dry garbage start')
    for n in range(5):
        th = threading.Thread(target=sayHello,args=('wxytest_%s'% n,))
        th.start()
    print('Dry garbage end')


if __name__ == '__main__':#The calling process must

    for n in range(2):
        mp = multiprocessing.Process(target=lajifenlei,)
        mp.start()
        print('process%s Started'%mp.pid)

    start_time = time.time()
    print(multiprocessing.active_children())
    while len(multiprocessing.active_children()) !=0:#Waiting for the completion of child process execution
        pass
    end_time = time.time()
    print('Time-consuming:%s'%(end_time-start_time))

//Test results:


5. Thread pool

import threadpool
import requests,hashlib
import threading

def down_load_file(url):
    r = requests.get(url)
    m = hashlib.md5(url.encode())
    file_name = m.hexdigest()
    print('Downloading%s'%file_name)
    with open('img/%s.jpg'% file_name,'wb') as fw:
        fw.write(r.content)

url_list = ['https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1564282263&di=9d1edb5e67c65051336a4ed2c7c5f56a&imgtype=jpg&er=1&src=http%3A%2F%2Fku.90sjimg.com%2Felement_origin_min_pic%2F18%2F03%2F27%2F1d7b7b1c20e2013963d677003e587421.jpg%21%2Ffwfh%2F804x804%2Fquality%2F90%2Funsharp%2Ftrue%2Fcompress%2Ftrue','https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1693892888,2363320737&fm=26&gp=0.jpg','https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563687763106&di=256bf12a6a322a5d161d9c430605eafa&imgtype=0&src=http%3A%2F%2Fhbimg.b0.upaiyun.com%2F02360ab0bb74f7a353a3d121d948d1e497b54dc61db49-bX40Ok_fw658']

pool = threadpool.ThreadPool(10)#Create a thread pool and start 10 threads in total. No matter how many threads are used, 10 threads will start.

reqs = threadpool.makeRequests(down_load_file,url_list)

# for req in reqs:
#     pool.putRequest(req)

[pool.putRequest(req) for req in reqs]
print(threading.active_count())
pool.wait()

print('End of the test')

//Test results:


6.jsonpath module (dictionary quick search key)

import jsonpath

d = {
    "stu":{
        "sex":'male',
        "house":{
            "beijing":{"Tetracyclic":5,"Tricyclic":4},
            "Shanghai":{"Pudong":4}
        }
    },
    "stu2":{
        "sex2":'female',
        "house":{
            "beijing":{"Second ring":6,"Tricyclic":8},
            "Hebei":{"Laiyuan":6}
        }
    }
}

# print(d['stu']['house']['Shanghai'])

r1 = jsonpath.jsonpath(d,'$.stu')#Get the current dictionary key(Class A key)by"beijing"Of vule,If not found, return false
print(r1)

r2 = jsonpath.jsonpath(d,'$.beijing')#Get the current dictionary key(Class A key)by"beijing"Of vule
print(r2)

result = jsonpath.jsonpath(d,'$..beijing')#Fuzzy query to get the current dictionary key(All levels key)by"beijing"All value
print(result)

//Test results:
[{'sex': 'male', 'house': {'beijing': {'Tetracyclic': 5, 'Tricyclic': 4}, 'Shanghai': {'Pudong': 4}}}]
False
[{'Tetracyclic': 5, 'Tricyclic': 4}, {'Second ring': 6, 'Tricyclic': 8}]

7.faker module (parameterized parameters can be quickly parameterized by this module)

import faker

f  = faker.Faker(locale='zh-CN')

print(dir(f))
print(f.name())
print(f.user_name())
print(f.md5())
print(f.hostname())
print(f.year())

//Test results:
['_Generator__config', '_Generator__format_token', '_Generator__random', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add_provider', 'address', 'am_pm', 'ascii_company_email', 'ascii_email', 'ascii_free_email', 'ascii_safe_email', 'bank_country', 'bban', 'binary', 'boolean', 'bothify', 'bs', 'building_number', 'catch_phrase', 'century', 'chrome', 'city', 'city_name', 'city_suffix', 'color_name', 'company', 'company_email', 'company_prefix', 'company_suffix', 'coordinate', 'country', 'country_code', 'credit_card_expire', 'credit_card_full', 'credit_card_number', 'credit_card_provider', 'credit_card_security_code', 'cryptocurrency', 'cryptocurrency_code', 'cryptocurrency_name', 'currency', 'currency_code', 'currency_name', 'date', 'date_between', 'date_between_dates', 'date_object', 'date_of_birth', 'date_this_century', 'date_this_decade', 'date_this_month', 'date_this_year', 'date_time', 'date_time_ad', 'date_time_between', 'date_time_between_dates', 'date_time_this_century', 'date_time_this_decade', 'date_time_this_month', 'date_time_this_year', 'day_of_month', 'day_of_week', 'district', 'domain_name', 'domain_word', 'ean', 'ean13', 'ean8', 'email', 'file_extension', 'file_name', 'file_path', 'firefox', 'first_name', 'first_name_female', 'first_name_male', 'first_romanized_name', 'format', 'free_email', 'free_email_domain', 'future_date', 'future_datetime', 'get_formatter', 'get_providers', 'hex_color', 'hexify', 'hostname', 'iban', 'image_url', 'internet_explorer', 'ipv4', 'ipv4_network_class', 'ipv4_private', 'ipv4_public', 'ipv6', 'isbn10', 'isbn13', 'iso8601', 'job', 'language_code', 'last_name', 'last_name_female', 'last_name_male', 'last_romanized_name', 'latitude', 'latlng', 'lexify', 'license_plate', 'linux_platform_token', 'linux_processor', 'local_latlng', 'locale', 'location_on_land', 'longitude', 'mac_address', 'mac_platform_token', 'mac_processor', 'md5', 'mime_type', 'month', 'month_name', 'msisdn', 'name', 'name_female', 'name_male', 'null_boolean', 'numerify', 'opera', 'paragraph', 'paragraphs', 'parse', 'password', 'past_date', 'past_datetime', 'phone_number', 'phonenumber_prefix', 'postcode', 'prefix', 'prefix_female', 'prefix_male', 'profile', 'provider', 'providers', 'province', 'pybool', 'pydecimal', 'pydict', 'pyfloat', 'pyint', 'pyiterable', 'pylist', 'pyset', 'pystr', 'pystruct', 'pytuple', 'random', 'random_choices', 'random_digit', 'random_digit_not_null', 'random_digit_not_null_or_empty', 'random_digit_or_empty', 'random_element', 'random_elements', 'random_int', 'random_letter', 'random_letters', 'random_lowercase_letter', 'random_number', 'random_sample', 'random_uppercase_letter', 'randomize_nb_elements', 'rgb_color', 'rgb_css_color', 'romanized_name', 'safari', 'safe_color_name', 'safe_email', 'safe_hex_color', 'seed', 'seed_instance', 'sentence', 'sentences', 'set_formatter', 'sha1', 'sha256', 'simple_profile', 'slug', 'ssn', 'street_address', 'street_name', 'street_suffix', 'suffix', 'suffix_female', 'suffix_male', 'text', 'texts', 'time', 'time_delta', 'time_object', 'time_series', 'timezone', 'tld', 'unix_device', 'unix_partition', 'unix_time', 'uri', 'uri_extension', 'uri_page', 'uri_path', 'url', 'user_agent', 'user_name', 'uuid4', 'windows_platform_token', 'word', 'words', 'year']
//Liang Rong
kzheng
e6ddc4998ec48de6065546f41bd89f8b
web-15.leili.cn
2016

Posted by pepperface on Tue, 23 Jul 2019 21:16:33 -0700