I'm doing a for loop and, because every iteration takes a bit of time, I thought to put the iteration inside a function and start it thanks to the threading module like this:
def myfunc(arg):
"""do something with arg, that takes a bit"""
for n in range(1, 1000000):
Thread(target = myfunc, args = (n, )).start()
Yes, I need to do a lot of iteration, but when I start the program (that obviously is way more complicated than the example I put here) it raise the exception RuntimeError: can't start new thread
.
So i changed my code as below to check when my computer can't start new threads like this:
def myfunc(arg):
"""do something with arg, that takes a bit"""
for index, n in enumerate(range(1, 1000000)):
try:
Thread(target = myfunc, args = (n, )).start()
except:
print(index)
exit()
Thanks to this I can understand how many iteration my for loop did and the output was, like the title said, 861 and yes, I tried to execute my code a lot of time to be sure: it's always 861. Now my question is: why exactly 861? There is a specific reason? And if this number have a reason, I can get it from a little python script (maybe a function) since I want this code to be executed on every possible computer? Sorry for the strange question.
P.S. I don't know if it's useful but I'll tell here: I have a 2700x processor, 8 cores and 16 threads.
Edit: as some of you required, here is the minimal reproducible example of my code below.
from threading import Thread
import socket
target = 'localhost'
def is_opened(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if s.connect_ex((target, port)) == 0:
return True
return False
def portscanner(ports):
opened = []
def _support(port):
print(port)
if is_opened(port):
opened.append(port)
threads = []
for port in ports:
t = Thread(target = _support, args = (port, ))
threads.append(t)
for thread in threads: # Here i tried with the enumerate
thread.start() # Here raise the RuntimeError
for thread in threads:
thread.join()
return opened
if __name__ == "__main__":
print(portscanner(range(49152, 65536)))
As you can see I'm trying to create a portscanner and if I apply the example I wrote up to my code I can see that as output I get 861. I don't want to utilize a limitated number of threads like 100 or 200 as someone said, I want instead to utilize the max power that a PC has to make this as fast as possible. Disclaimer: I know port scanning is illegal but I'm trying this only for learn and for personal exercise. I will never scan others network, server, service or similar.