当前位置:网站首页>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
边栏推荐
- 苹果硬件技术高级副总裁揭露:自研 M1 太难了
- Suffix Array
- Motor control - speed loop design
- Digital IT operation from the perspective of thinking transformation
- Matlab GUI -- scrollbar (animation demonstration)
- Topological sorting & Critical Path & Digital DP
- 我这边需要写两个接受数据的接口,然后把数据存入数据库,完成数据持久化
- Kubernetes 中数据包的生命周期 -- 第 1 部分
- Teach you by hand: rolling bearing fault diagnosis based on deep learning
- Installing NFS file system
猜你喜欢

你的思维会改变你的行为,你的行为会改变你的境遇

教你轻松解决CSRF跨站请求伪造攻击
![[nonlinear control theory] 1_ Lyapunov direct method](/img/ad/68bceb288d40ae98b60dbb83e0b91d.png)
[nonlinear control theory] 1_ Lyapunov direct method

美国加息人民币贬值,这类产品却迎来蜜月期

Motor control - speed loop design

连接服务器报错No supported authentication methods available

IoT平台如何实现业务配置中心

【生活中的逻辑谬误】对人不对事和两难陷阱

犀牛软件插件-rhino插件-visual studio-创建你的第一个插件

Bucket sorting ← C language implementation
随机推荐
MATLAB---进度条动画演示
hyperf 执行sql语句,参数会有两个单引号
左程云 - 大厂刷题班 - 绳子覆盖最多的点
Kubernetes 中数据包的生命周期 -- 第 1 部分
P4 Tutorials---- source routing
Use of laravel redis
我这边需要写两个接受数据的接口,然后把数据存入数据库,完成数据持久化
Browser plug-in (BD new tab) wallpaper appreciation
RMQ & segment tree review
Review suffix array & automata
Special training of AC automata
(coordinate dynamic programming) lintcode medium 248 · count the number of numbers smaller than a given integer
MATLAB GUI--ScrollBar(动画演示)
左程云 - 大厂刷题班 - 一种字符在左,另一种字符在右的最少交换次数
拓扑排序&关键路径&数位dp
Six practices of Windows operating system security attack and defense
从B站和小红书看,如何做好社区产品?
Question brushing & Competition & Review
org.apache.flink.client.deployment.ClusterDeploymentException: Could not deploy Yarn job cluster.
Intelligent party building platform system development, and promote the construction of "Internet plus party building"