Python 3.8.2在VS代码中出错-无法连接到-在start_客户端中

Python 3.8.2在VS代码中出错-无法连接到-在start_客户端中,python,visual-studio-code,Python,Visual Studio Code,我有一个运行的多线程程序: Windows 10 PRO x64 Python 3.8.2(x64)(Python 3.8.2 (tags/v3.8.2:7b3ab59,2020年2月25日,23:03:10)[MSC v.1916 64位 (win32上的AMD64)]) 也可以尝试Python 3.8.0,但出现相同错误 VS代码(x64)1.43.0 ms-pythonVS代码扩展(ms-python.python-2020.2.64397) 我遇到了这个错误: Could not c

我有一个运行的多线程程序:

  • Windows 10 PRO x64
  • Python 3.8.2(x64)(Python 3.8.2 (tags/v3.8.2:7b3ab59,2020年2月25日,23:03:10)[MSC v.1916 64位 (win32上的AMD64)])
  • 也可以尝试Python 3.8.0,但出现相同错误
  • VS代码(x64)1.43.0
  • ms-pythonVS代码扩展(ms-python.python-2020.2.64397)
我遇到了这个错误:

Could not connect to 127.0.0.1: 63323
Could not connect to 127.0.0.1: 63323
Traceback (most recent call last):
Traceback (most recent call last):
  File "c:\Users\test\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 514, in start_client
    s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be established because the target computer actively rejected it
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\Users\test\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 514, in start_client
    s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be established because the target computer actively rejected it
Traceback (most recent call last):
  File "c:\Users\test\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\_vendored\pydevd\pydevd.py", line 2536, in settrace
  File "<string>", line 1, in <module>
  File "c:\Users\test\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\_vendored\pydevd\pydevd.py", line 2536, in settrace
Could not connect to 127.0.0.1: 63323
    _locked_settrace(
    _locked_settrace(  File "c:\Users\test\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\_vendored\pydevd\pydevd.py", line 2610, in _locked_settrace
Could not connect to 127.0.0.1: 63323
从functions.py文件:

import sys
import csv
import time
import datetime
import argparse
import itertools as it
from os import system, name
from enum import Enum, unique
from tqdm import tqdm
from math import ceil
from multiprocessing import Pool, cpu_count
import codecs
程序在另一台使用Python 3.8.0的PC上运行得很好,我不理解这个错误

这个程序只比较2个文件,并显示差异,他们不使用任何连接到另一台服务器或互联网


唯一的区别是我现在使用的是Intel i9-9900(8c/16t),而在第二台计算机上使用的是仅4核的i5-7500


编辑

当我将内核数从16设置为8时,程序运行不会出错。我的处理器有8个物理核和16个逻辑核,我使用cpu_count()检查cpu的数量,如:

threads = 8 #cpu_count()
p = Pool(threads)
问题在哪里


编辑-2020年3月9日-源代码

main.py

import functions as fnc
from multiprocessing import freeze_support


# Run main program
if __name__ == '__main__':
    freeze_support()

    fnc.main()
import sys
import csv
import time
import datetime
import argparse
import itertools as it
from os import system, name
from enum import Enum, unique
from tqdm import tqdm
from math import ceil
from multiprocessing import Pool, cpu_count
import codecs


# ENUM
@unique
class Type(Enum):
    TT = 1


# CLASS
class TextFormat:
    PURPLE = '\033[95m'
    CYAN = '\033[96m'
    DARKCYAN = '\033[36m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    END = '\033[0m'


class CrossReference:
    def __init__(self, pn, comp, comp_name, type, diff):
        self.pn = pn
        self.comp = comp
        self.comp_name = comp_name
        self.type = type
        self.diff = diff

    def __str__(self):
        return f'{self.pn} {get_red("|")} {self.comp} {get_red("|")} {self.comp_name} {get_red("|")} {self.type} {get_red("|")} {self.diff}\n'

    def __repr__(self):
        return str(self)

    def getFullRow(self):
        return self.pn + ';' + self.comp + ';' + self.comp_name + ';' + self.type + ';' + self.diff + '\n'


class CrossDuplication:
    def __init__(self, pn, comp, cnt):
        self.pn = pn
        self.comp = comp
        self.cnt = cnt

    def __str__(self):
        return f'{self.pn};{self.comp};{self.cnt}\n'

    def __repr__(self):
        return str(self)

    def __hash__(self):
        return hash(('pn', self.pn,
                 'competitor', self.comp))

    def __eq__(self, other):
        return self.pn == other.pn and self.comp == other.comp 


# FUNCTIONS
def get_formated_time(mili):
    sec = mili / 1000.0
    return str(datetime.timedelta(seconds = sec)) 

def get_green(text):    # return red text
    return(TextFormat.GREEN + str(text) + TextFormat.END)


def get_red(text):      # return red text
    return(TextFormat.RED + str(text) + TextFormat.END)


def get_yellow(text):   # return yellow text
    return(TextFormat.YELLOW + str(text) + TextFormat.END)


def get_blue(text):     # return blue text
    return(TextFormat.BLUE + str(text) + TextFormat.END)


def get_bold(text):     # return bold text format
    return(TextFormat.BOLD + str(text) + TextFormat.END)


def print_info(text):   # print info text format
    print("=== " + str(text) + " ===")


# ### LOADER ### Load Cross Reference file
def CSVCrossLoader(file_url, type):
    try:
        print(get_yellow("============ LOAD CROSS CSV DATA ==========="))

        print_info(get_green(f"Try to load data from {file_url}"))
        destination = []
        with open(file_url, encoding="utf-8-sig") as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=';')
            line_count = 0
            for row in csv_reader:
                if row[0].startswith('*'):
                    continue
                if Type[row[3]] is not type:
                    continue
                cr = CrossReference(row[0], row[1], row[2], row[3], row[4])
                destination.append(cr)
                line_count += 1
            filename = file_url.rsplit('\\', 1)
            print(
                f'Processed {get_red(line_count)} lines for {get_red(type.name)} from {filename[1]}')
            print_info(get_green(f"Data was loaded successfully"))
            return destination
    except Exception as e:
        print(e)
        print_info(get_red(f"File {file_url} could not be loaded"))
        print_info(get_red("Program End"))
        exit(0)


# ### LOADER ### Load Catalog with PN details (load only first row)
def CSVCatalogLoader(file_url):
    try:
        print(get_yellow("=========== LOAD CATALOG CSV DATA =========="))
        print_info(get_green(f"Try to load data from {file_url}"))
        destination = []
        with open(file_url, encoding="utf-8-sig") as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=';')
            line_count = 0
            for row in csv_reader:
                if row[0].startswith('*'):
                    continue
                destination.append(row[0])
                line_count += 1
            filename = file_url.rsplit('\\', 1)
            print(f'Processed {get_red(line_count)} lines from {filename[1]}')
            print_info(get_green(f"Data was loaded successfully"))
            return destination
    except:
        print_info(get_red(f"File {file_url} could not be loaded"))
        print_info(get_red("Program End"))
        exit(0)



def FindDuplications(tasks):
    dlist, start, count = tasks

    duplicates = []
    for r in tqdm(dlist[start:start + count]):
        matches = [x for x in dlist if r.pn == x.pn and r.comp == x.comp]
        duplicates.append(CrossDuplication(r.pn, r.comp, len(matches)))

    return {d for d in duplicates if d.cnt > 1}


def CheckDuplications(cross_list):
    threads = cpu_count()
    tasks_per_thread = ceil(len(cross_list) / threads)

    tasks = [(cross_list, tasks_per_thread * i, tasks_per_thread) for i in range(threads)]

    p = Pool(threads)
    duplicates = p.map(FindDuplications, tasks)
    p.close()
    p.join() 

    duplicates = {item for sublist in duplicates for item in sublist}
    return duplicates   




def main():

    # Main Title of app
    print_info(get_yellow("Run app"))


    # VARIABLES
    catalog_list = []
    cross_list = []


    # Start calculate program running time
    start_time = int(round(time.time() * 1000))


    # load URL param from program input arguments
    validation_type = Type[sys.argv[1]]
    cross_ref_url = sys.argv[2]
    catalog_url = sys.argv[3]


    # Get info abou tested type
    print_info(get_blue(f"|||   Validate data for {validation_type.name}   |||"))
    print("Number of processors: ", cpu_count())
    print()


    # load data
    cross_list = CSVCrossLoader(cross_ref_url, validation_type)
    catalog_list = CSVCatalogLoader(catalog_url)

    # Chech data in Cross Reference for Duplications [ MULTITHREAD ]
    duplicates = CheckDuplications(cross_list)

    # Print duration of execution script
    mili = int(int(round(time.time() * 1000)) - start_time)
    print(f'Script duration - {mili} ms | {get_formated_time(mili)}')


    # End of program
    print_info(get_yellow(""))
    print()
函数.py

import functions as fnc
from multiprocessing import freeze_support


# Run main program
if __name__ == '__main__':
    freeze_support()

    fnc.main()
import sys
import csv
import time
import datetime
import argparse
import itertools as it
from os import system, name
from enum import Enum, unique
from tqdm import tqdm
from math import ceil
from multiprocessing import Pool, cpu_count
import codecs


# ENUM
@unique
class Type(Enum):
    TT = 1


# CLASS
class TextFormat:
    PURPLE = '\033[95m'
    CYAN = '\033[96m'
    DARKCYAN = '\033[36m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    END = '\033[0m'


class CrossReference:
    def __init__(self, pn, comp, comp_name, type, diff):
        self.pn = pn
        self.comp = comp
        self.comp_name = comp_name
        self.type = type
        self.diff = diff

    def __str__(self):
        return f'{self.pn} {get_red("|")} {self.comp} {get_red("|")} {self.comp_name} {get_red("|")} {self.type} {get_red("|")} {self.diff}\n'

    def __repr__(self):
        return str(self)

    def getFullRow(self):
        return self.pn + ';' + self.comp + ';' + self.comp_name + ';' + self.type + ';' + self.diff + '\n'


class CrossDuplication:
    def __init__(self, pn, comp, cnt):
        self.pn = pn
        self.comp = comp
        self.cnt = cnt

    def __str__(self):
        return f'{self.pn};{self.comp};{self.cnt}\n'

    def __repr__(self):
        return str(self)

    def __hash__(self):
        return hash(('pn', self.pn,
                 'competitor', self.comp))

    def __eq__(self, other):
        return self.pn == other.pn and self.comp == other.comp 


# FUNCTIONS
def get_formated_time(mili):
    sec = mili / 1000.0
    return str(datetime.timedelta(seconds = sec)) 

def get_green(text):    # return red text
    return(TextFormat.GREEN + str(text) + TextFormat.END)


def get_red(text):      # return red text
    return(TextFormat.RED + str(text) + TextFormat.END)


def get_yellow(text):   # return yellow text
    return(TextFormat.YELLOW + str(text) + TextFormat.END)


def get_blue(text):     # return blue text
    return(TextFormat.BLUE + str(text) + TextFormat.END)


def get_bold(text):     # return bold text format
    return(TextFormat.BOLD + str(text) + TextFormat.END)


def print_info(text):   # print info text format
    print("=== " + str(text) + " ===")


# ### LOADER ### Load Cross Reference file
def CSVCrossLoader(file_url, type):
    try:
        print(get_yellow("============ LOAD CROSS CSV DATA ==========="))

        print_info(get_green(f"Try to load data from {file_url}"))
        destination = []
        with open(file_url, encoding="utf-8-sig") as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=';')
            line_count = 0
            for row in csv_reader:
                if row[0].startswith('*'):
                    continue
                if Type[row[3]] is not type:
                    continue
                cr = CrossReference(row[0], row[1], row[2], row[3], row[4])
                destination.append(cr)
                line_count += 1
            filename = file_url.rsplit('\\', 1)
            print(
                f'Processed {get_red(line_count)} lines for {get_red(type.name)} from {filename[1]}')
            print_info(get_green(f"Data was loaded successfully"))
            return destination
    except Exception as e:
        print(e)
        print_info(get_red(f"File {file_url} could not be loaded"))
        print_info(get_red("Program End"))
        exit(0)


# ### LOADER ### Load Catalog with PN details (load only first row)
def CSVCatalogLoader(file_url):
    try:
        print(get_yellow("=========== LOAD CATALOG CSV DATA =========="))
        print_info(get_green(f"Try to load data from {file_url}"))
        destination = []
        with open(file_url, encoding="utf-8-sig") as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=';')
            line_count = 0
            for row in csv_reader:
                if row[0].startswith('*'):
                    continue
                destination.append(row[0])
                line_count += 1
            filename = file_url.rsplit('\\', 1)
            print(f'Processed {get_red(line_count)} lines from {filename[1]}')
            print_info(get_green(f"Data was loaded successfully"))
            return destination
    except:
        print_info(get_red(f"File {file_url} could not be loaded"))
        print_info(get_red("Program End"))
        exit(0)



def FindDuplications(tasks):
    dlist, start, count = tasks

    duplicates = []
    for r in tqdm(dlist[start:start + count]):
        matches = [x for x in dlist if r.pn == x.pn and r.comp == x.comp]
        duplicates.append(CrossDuplication(r.pn, r.comp, len(matches)))

    return {d for d in duplicates if d.cnt > 1}


def CheckDuplications(cross_list):
    threads = cpu_count()
    tasks_per_thread = ceil(len(cross_list) / threads)

    tasks = [(cross_list, tasks_per_thread * i, tasks_per_thread) for i in range(threads)]

    p = Pool(threads)
    duplicates = p.map(FindDuplications, tasks)
    p.close()
    p.join() 

    duplicates = {item for sublist in duplicates for item in sublist}
    return duplicates   




def main():

    # Main Title of app
    print_info(get_yellow("Run app"))


    # VARIABLES
    catalog_list = []
    cross_list = []


    # Start calculate program running time
    start_time = int(round(time.time() * 1000))


    # load URL param from program input arguments
    validation_type = Type[sys.argv[1]]
    cross_ref_url = sys.argv[2]
    catalog_url = sys.argv[3]


    # Get info abou tested type
    print_info(get_blue(f"|||   Validate data for {validation_type.name}   |||"))
    print("Number of processors: ", cpu_count())
    print()


    # load data
    cross_list = CSVCrossLoader(cross_ref_url, validation_type)
    catalog_list = CSVCatalogLoader(catalog_url)

    # Chech data in Cross Reference for Duplications [ MULTITHREAD ]
    duplicates = CheckDuplications(cross_list)

    # Print duration of execution script
    mili = int(int(round(time.time() * 1000)) - start_time)
    print(f'Script duration - {mili} ms | {get_formated_time(mili)}')


    # End of program
    print_info(get_yellow(""))
    print()
launch.json-VS代码配置文件

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "First",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "args": [
                "TT",
                "..\\url\\ref.csv",
                "..\\url\\catalog.csv"
            ]
        }
    ]
}
数据文件-ref.csv(示例)

数据文件-catalog.csv(示例)

应用程序加载2个CSV文件,并在ref.CSV中查找重复的行,该文件中的行数超过100k+行,并将每行的第一列和第二列与foreach循环中的相同数据进行比较


编辑-2020年3月10日-第三台计算机

今天我在我的笔记本电脑(联想T480s)上试用了它,它采用了英特尔酷睿i7-8550U和4c/8t

我使用
threads=cpu\u count()
运行它,此函数返回8个内核/线程,一切正常,配置与前两台电脑相同,但仅在Intel Core i9-9900上出现代码获取错误

另外,我在i9-9900上试过:

threads = 8   # OK
threads = 12  # OK
threads = 14  # ERROR
threads = 16  # ERROR
=========================================== 在CMD或Powershell中运行本机可以使用16个线程正常运行-OK

C:\Users\test\AppData\Local\Programs\Python\Python38\python.exe 'c:\Users\test\Documents\Work\Sources\APP\src\APP\cross-validator.py' 'TT' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\APP\Definitions\ref.csv' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\APP\Definitions\Tan\catalog.csv'
通过VS代码调试运行添加一个带有ms python链接的参数-错误

 ${env:PTVSD_LAUNCHER_PORT}='49376'; & 'C:\Users\test\AppData\Local\Programs\Python\Python38\python.exe' 'c:\Users\test\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\launcher' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\cross-validator.py' 'TAN' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\APP\Definitions\ref.csv' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\APP\Definitions\Tan\catalog.csv'

感谢您的帮助

检查您的本地主机设置。或者您的63323端口正忙于其他程序。

该端口上可能没有运行任何程序,或者防火墙阻止了连接。好的,但为什么我需要连接?在第二台PC上,我没有LocalHost的任何服务器或以太网设置。看起来您正在运行调试模式,并且正在连接到调试服务器。检查调试器是否设置正确。好的,现在我尝试将处理器数设置为常量set 8,它可以正常工作。我有8核16线程的Intel Core i9 9900,但16线程不起作用,但我在第三台有6核12线程的i7-8700的计算机上尝试了该应用程序,结果都很好。祝你好运!我不太熟悉VS studio,我使用空闲软件,避免使用第三方软件。我不使用远程调试,也不在这个端口上运行,请检查我的编辑部分
 ${env:PTVSD_LAUNCHER_PORT}='49376'; & 'C:\Users\test\AppData\Local\Programs\Python\Python38\python.exe' 'c:\Users\test\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\no_wheels\ptvsd\launcher' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\cross-validator.py' 'TAN' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\APP\Definitions\ref.csv' 'c:\Users\test\Documents\Work\Sources\APP\src\APP\APP\Definitions\Tan\catalog.csv'