并发和并行有什么区别?
赞赏的例子.
在决定从current.futures在ThreadPoolExecutor中将max_workers设置为什么时,应考虑哪些因素?
只要您可以期望Python 3.5+可用,是否有任何理由不将max_workers设置为None,然后将“默认设置为计算机上的处理器数量乘以5”,如此处的文档所述? https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
我正在编写一个程序,需要扫描主机中的所有65535个端口,搜索那些打开的端口.这是我到目前为止,它的工作原理,但每次执行脚本时都会产生不同的结果,为什么会这样?
def check_open_port(host, port):
s = socket.socket()
s.settimeout(0.1)
# the SO_REUSEADDR flag tells the kernel to reuse a local
# socket in TIME_WAIT state, without waiting for its natural
# timeout to expire.
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
code = s.connect_ex((host, port))
s.close()
if code == 0:
return True
else:
return False
except socket.error:
return False
def get_open_ports(host, max_port=65535):
open_ports = []
def worker(port):
if check_open_port(host, port):
open_ports.append(port)
pool = ThreadPoolExecutor(max_workers=10000)
[pool.submit(worker, port) for port in range(1, max_port + …Run Code Online (Sandbox Code Playgroud)