Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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_Multithreading_Python Multithreading - Fatal编程技术网

同时运行多个线程,但逐个打印python?

同时运行多个线程,但逐个打印python?,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,因为你可能知道我在说什么。让我更清楚地解释一下 举个例子:我有四个线程,都是模拟运行的 th1 = threading.Thread(target=connCheck, args= (newurl, schemess, 21)) th2 = threading.Thread(target=connCheck, args=( newurl, schemess, 22)) th3 = threading.Thread(target=connCheck, args= (newurl, schem

因为你可能知道我在说什么。让我更清楚地解释一下

举个例子:我有四个线程,都是模拟运行的

th1 = threading.Thread(target=connCheck,  args= (newurl, schemess, 21))
th2 = threading.Thread(target=connCheck,  args=( newurl, schemess, 22))
th3 = threading.Thread(target=connCheck,  args= (newurl, schemess,80))
th4 = threading.Thread(target=connCheck,  args=(newurl, schemess,8080))
(这是用于扫描端口号为21、22、80、8080的端口的扫描仪。)

我像这样运行这些线程:-

th1.start()
th2.start()
th3.start()
th4.start()
th4.join()
List of port open:-

[+]21 
[+]22 [+]80
[+]8080
注意:-ConnConCheck是一个告知哪些端口打开的功能

因此,在我的终端中,它显示两个端口都是打开的,但是的方式非常糟糕。有时候是这样印的

List of port open:-

[+]21 [+]22 [+]80
[+]8080
有时它会打印成这样:-

th1.start()
th2.start()
th3.start()
th4.start()
th4.join()
List of port open:-

[+]21 
[+]22 [+]80
[+]8080
有时它会以其他方式打印

所以我想让他们用一种好的方式,像逐行打印。但最重要的是要记住,我希望同时运行上述所有线程,这样就不会影响扫描速度

所以我是来问你的。所以请任何人带我进来

谢谢

[编辑]:-以下是ConnConCheck函数:-

try: 

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(3)

        result = sock.connect_ex((ip, port))

        newip = schemess+ip+slash

        if port == 21:
            if result == 0:

                print "\t [+]" + str(port)
                ftpurls.append(newip)
                dicta(newip, port)


        else:
            if result == 0:


                    print '\t' + str(port)+ '\n'

                    dicta(newip, port)                      


        sock.close()
    except Exception as e:
        print str(e) + '\n'

请忽略dicta函数。

有几种方法可以实现此功能,使用锁,另一种方法是使用
日志记录
模块。我一直在使用
threading.Lock()
编写多线程代码,但最近我发现
日志记录更易于使用

lock = threading.Lock()
with lock:
     print("...")
with lock
在进入时调用
lock.acquire()
,在退出时调用
lock.release()
。或者你可以手动调用它们。但一定要在收购后释放它,否则你将面临死锁

另一种更简单的方法是使用我提到的
日志记录<代码>日志记录
库是线程安全的,这意味着任何线程都可以同时使用这些函数。使用此模块还允许您将日志写入日志文件

import logging

logging.basicConfig(format='%(message)s')
logging.info("...")

你能发布你的ConnConCheck函数吗?使用队列。只让一个线程(主线程)打印队列中的项目。检查-这是使用
多处理
时更复杂的情况,但是第一个示例(处理输出的主进程/线程)在多线程场景中也能很好地工作。由于多线程支持共享内存,您可以通过共享一些结构而不是返回和收集结果来实现更简单的操作。您可以像这样使用
ThreadPoolExecutor
,将ThreadPoolExecutor()作为t:用于t.map中的数据(connCheck,repeat(newurl),repeat(newurl),list_port,):…