首页 > 解决方案 > 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 函数时它只会使用一个内核。我不知道这是网络问题,应该使用线程或者我的代码是否错误。我也尝试在进程启动后直接加入进程,并将加入语句放在不同的位置,但这没有奏效。

标签: pythonnetworkingmultiprocessingtor

解决方案


推荐阅读