Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中并行运行代码块_Python_Python 3.x_Exception_Urllib_Try Except - Fatal编程技术网

如何在python中并行运行代码块

如何在python中并行运行代码块,python,python-3.x,exception,urllib,try-except,Python,Python 3.x,Exception,Urllib,Try Except,我正在测试大量的设备,因此我正在编写一个测试程序,这样我就可以自动完成任务。 请告诉我,因为我是Python或任何编程语言的新手。 我希望能够并行运行try-and-except代码块,这样我就不必为每一个检查的新设备等待10秒(图片超过1000个设备)。 所以基本上: try: preflash = urllib.request.urlopen("http://10.10.10.2", timeout=10).getcode() print("We

我正在测试大量的设备,因此我正在编写一个测试程序,这样我就可以自动完成任务。 请告诉我,因为我是Python或任何编程语言的新手。 我希望能够并行运行try-and-except代码块,这样我就不必为每一个检查的新设备等待10秒(图片超过1000个设备)。 所以基本上:

try:
    preflash = urllib.request.urlopen("http://10.10.10.2", timeout=10).getcode()
    print("Web page status code:", preflash)
    print("FAIL")
    sys.exit(0)
except urllib.error.URLError: 
    correct = urllib.request.urlopen("http://192.168.100.5", timeout=10).getcode()
    print("Web page status code:", correct)
    print("IP address: 192.168.100.5 is reachable")
这个代码块应该并行运行。如果10.10.10.2首先可访问,则它将关闭,但如果192.168.100.5在此之前可访问,则它应继续执行程序

以下是输出的外观,仅供参考

非常感谢任何帮助,请对我放松,伙计们,干杯

print(100*"#")

try:
    preflash = urllib.request.urlopen("http://10.10.10.2", timeout=10).getcode()
    print("Web page status code:", preflash)
    print("FAIL")
    sys.exit(0)
except urllib.error.URLError: 
    correct = urllib.request.urlopen("http://192.168.100.5", timeout=10).getcode()
    print("Web page status code:", correct)
    print("IP address: 192.168.100.5 is reachable")

 
print(100*"#")
print("Fetching device variables")
time.sleep(3)
print(100*"-")    
# Declare url String    
url_str = 'http://192.168.100.2/globals.xml'

# open webpage and read values
xml_str = urllib.request.urlopen(url_str).read()

# Parses XML doc to String for Terminal output
xmldoc = minidom.parseString(xml_str)

# Finding the neccassary Set points/ Sollwerte from the xmldoc

time.sleep(0.5)
# prints the order_number from the xmldoc
order_number = xmldoc.getElementsByTagName('order_number')
odr_nmr = order_number[0].firstChild.nodeValue
print("The Order number of the current device is:", odr_nmr)
print(100*"-")

听起来像是一个线程化的工作,了解内核以及线程在python中的工作原理。试着弄清楚。