当前位置:网站首页>Simple threading - using threads to run multiple browsers easily

Simple threading - using threads to run multiple browsers easily

2022-04-21 11:14:00 There is nothing else to do with peace and security

         This is Qing'an , Let's learn about threads in this chapter .

         What is thread ? There are many official articles written on the Internet, belonging to , It's true. It's a little dazzling , Also very ignorant force .

         To put it simply , Threads are the basic unit of processor scheduling , It has something to do with the process . A program has at least one process , A process has at least one thread .. It's like I took the high-speed railway from Shenzhen to Beijing today , Is there a direct , This direct can be seen as a process , We can also transfer from Shenzhen to Beijing , These transfer lines can be seen as threads .

        The modules that need to be used are threading!

         First, let's write a single thread .

import threading
from selenium import webdriver



def work():
    fox = webdriver.Firefox()
    fox.get("https://www.baidu.com")
    fox.quit()


if __name__ == '__main__':
    th = threading.Thread(target=work)
    th.start()

         The above is a single thread , Actually ,python It's a single thread mechanism , therefore , Even if it's not written like this , Running is still a single thread .

        The thread needs to tell it which function you need to run , And then through start Method , Run . In this way, we completed a single thread .

         So let's have multiple functions :

# -->>> Qing'an <<<---
import threading
from selenium import webdriver



def work():
    fox = webdriver.Firefox()
    fox.get("https://www.baidu.com")
    fox.quit()


def time_():
    fox = webdriver.Chrome()
    fox.get("https://www.jd.com/")
    fox.quit()


def time_1():
    fox = webdriver.Chrome()
    fox.get("https://www.sina.com.cn/")
    fox.quit()


if __name__ == '__main__':
    th = threading.Thread(target=work)
    th1 = threading.Thread(target=time_)
    th2 = threading.Thread(target=time_1)

    for i in (th, th1, th2):
        i.start()
    for j in (th, th1, th2):
        j.join()

        Multithreading , In the above code, we created three threads , Namely th,th1,th2, Then use the for Loop to start the multithreading , So how do I know if it's really started with multithreading .

        There are methods in multithreading to print out the corresponding thread id:

    print(threading.currentThread().ident)
    print(f"ident:{threading.get_ident()}")

        Both can , How to write it? ?

def time_():
    fox = webdriver.Chrome()
    print(threading.currentThread().ident)
    print(f"ident:{threading.get_ident()}")
    fox.get("https://www.jd.com/")
    fox.quit()

        Empathy , Can be written in , After running, you will see the thread ID 了 .

        In the above, we also use join Method , What's the use of this ?

         wait for , Blocking threads , Until the thread is executed , Just execute the following code . In the above code , The use of join It's not very obvious . Let's take another example :

# -->>> Qing'an <<<---
import threading
import time
import random


def work_a():
    for i in range(5):
        print(i, "work a", threading.get_ident())
        time.sleep(random.random() * 2)
    return "work a"


def work_b():
    for i in range(5):
        print(i, "work b", threading.get_ident())
        time.sleep(random.random() * 2)
    return "work b"


if __name__ == '__main__':
    th = threading.Thread(target=work_b)
    th1 = threading.Thread(target=work_a)
    th.start()
    th.join()

        here , You can try it yourself , hold join Get rid of , Add one with jion. It is obvious that , Use cases join Method , In fact, it is a bit similar to the concept of single thread . But there is no denying that it does enable two threads , But it's a thread that runs , Another thread starts again .

版权声明
本文为[There is nothing else to do with peace and security]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211110027636.html