python - Python 多处理仅在一个带有网络的核心上工作
问题描述
我正在制作一个程序来生成一个 .onion url,然后测试该 url 以查看它是否存在。问题是我已经将它设置为使用多处理并且它只使用一个核心,即使在多个进程正在运行时也是如此。
我的代码是
import requests
import time
import sys
import threading
import random
import string
import multiprocessing
proxies = {
'http': 'socks5h://127.0.0.1:9150',
'https': 'socks5h://127.0.0.1:9150'
}
try:
print("Your hidden ip is: " + requests.get("https://api.ipify.org/?", proxies=proxies, timeout=10).text)
print("Checks at intervals to check if the commection is still established (pings google.com)")
except Exception as e:
print("Make sure tor is running or check that a socks proxy is running at 127.0.0.1:9150!")
sys.exit(0)
def checkSite(site):
if random.randint(0, 100) == 5:
site = "https://google.com"
try:
data = requests.get(site, proxies=proxies, timeout=10)
if data.status_code == 200:
print('Website exists {}'.format(site))
sys.exit(0)
print("hello")
else:
return
#print('Website does not exist {}'.format(site))
except Exception:
#print('Website does not exist {}'.format(site))
return
def genSite():
site = ""
x= string.ascii_lowercase + "234562"
for i in range(0, 16):
site = site + x[random.randint(0, len(x) - 1)]
return ("http://" + site + ".onion")
def findSite():
while True:
checkSite(genSite())
if __name__ == "__main__":
processes = []
for _ in range(int(input("Enter number of processes: "))):
p = multiprocessing.Process(target=findSite) # create a new Process
processes.append(p)
p.start()
for process in processes:
process.join()
我已经尝试过运行阶乘函数,这将利用所有内核,但是在运行 findSite 函数时它只会使用一个内核。我不知道这是网络问题,应该使用线程或者我的代码是否错误。我也尝试在进程启动后直接加入进程,并将加入语句放在不同的位置,但这没有奏效。
解决方案
推荐阅读
- google-chrome - 无法提交内部错误 - Chrome 商店扩展提交
- javascript - 使用循环从数组中删除第一个元素,但在 Javascript 中存储“未定义”的最后一个元素
- reactjs - 将环境变量添加到 React 项目
- amazon-web-services - Kinesis Data Firehose 源“Direct PUT”与“Kinesis Data Stream”
- java - 如果配置文件设置不更改为 application-default.properties,如何忽略 application.properties 值
- r - 如何获取多个链接的文本
- video - 在m210 v2中,根据usb模式收不到视频
- laravel - 将数组传递给 Laravel Blade 指令
- java - 无法使用 Apache POI 按正确顺序提取 docx TableCell 的所有元素
- mysql - 查询 datetime_add 和 datetime_sub 哪个更快?