将python 3多线程输出到CSV文件(空白)

克隆工厂

我是python的新手,我从遇到的教程中获得了此多线程功能。不确定是否可以练习。

我要存档的内容:ping主机名列表,然后向上或向下返回。将结果写入CSV文件

该文件当前执行的操作是:ping主机名列表并向上或向下返回。它创建的csv文件为空,并且似乎没有向其写入任何结果。

我进行了一些测试,发现使用pings multithreadin进行串行代码处理对我来说快了大约16倍。我正在做大量的ping大约9000,并希望它们尽快返回。

您能告诉我我在csv部分出错了吗?

import threading
from queue import Queue
import time
import subprocess as sp
import csv

# lock to serialize console output
lock = threading.Lock()

def do_work(item):
    #time.sleep(1) # pretend to do some lengthy work.
    # Make sure the whole print completes or threads can mix up output in one line.

    status,result = sp.getstatusoutput("ping -n 3 " + str(item))
    if status == 0:
        result = 'Up'
    else:
        result = 'Down'

    with lock:
        output.writerow({'hostname': item,'status': result})
        array.append({'hostname': item,'status': result})
        print(threading.current_thread().name,item,result)

# The worker thread pulls an item from the queue and processes it
def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

# Create the queue and thread pool.
q = Queue()
for i in range(100):
     t = threading.Thread(target=worker)
     t.daemon = True  # thread dies when main thread (only non-daemon thread) exits.
     t.start()

array = []
# stuff work items on the queue (in this case, just a number).
start = time.perf_counter()

headers = ['status','hostname']
output = csv.DictWriter(open('host-export.csv','w'), delimiter=',', lineterminator='\n', fieldnames=headers)
output.writeheader()

txt = open("hosts.txt", 'r', encoding="utf8")
for line in txt:
    q.put(line,array)

q.join()       # block until all tasks are done

# "Work" took .1 seconds per task.
# 20 tasks serially would be 2 seconds.
# With 4 threads should be about .5 seconds (contrived because non-CPU intensive "work")
print(array)

print('time:',time.perf_counter() - start)

我还为csv添加了大量文字,以为我可能无法访问该函数中的csv对象,但这也无法正常工作,如下所示。

headers = ['status','hostname']
output = csv.DictWriter(open('host-export.csv','w'), delimiter=',', lineterminator='\n', fieldnames=headers)
output.writeheader()
output.writerows(array)
克隆工厂

我弄错了我做错了什么。我没有关闭文件连接,所以它没有写入文件。

这是我现在用来定位我的csv文件的代码。

fieldnames = ['ip', 'dns', 'pings'] #headings 
test_file = open('test2-p.csv','w', newline='') #open file
csvwriter = csv.DictWriter(test_file, delimiter=',', fieldnames=fieldnames) #set csv writing settings
csvwriter.writeheader() #write csv headings 
for row in rows: #write to csv file
     csvwriter.writerow(row)
test_file.close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

python:将列表输出到csv文件

来自分类Dev

将groupby输出到csv文件熊猫

来自分类Dev

Python Scrapy不输出到csv文件

来自分类Dev

python3多线程写入文件

来自分类Dev

将多线程输出保存到txt文件

来自分类Dev

将python命令结果输出到文件

来自分类Dev

Python - 将变量输出到 TXT 文件

来自分类Dev

将python命令结果输出到文件

来自分类Dev

将csv文件输出到S3存储桶中的特定路径

来自分类Dev

当我尝试将数据帧输出到csv文件时,为什么我的输出仅一行?Python3 / boto3

来自分类Dev

Python输出到CSV

来自分类Dev

Python输出到CSV

来自分类Dev

如何使用python使用scrapy将多个网页抓取的数据输出到csv文件中

来自分类Dev

如何将PostgreSQL查询输出导出到csv文件

来自分类Dev

Python:使用unicodecsv将Unicode输出到csv

来自分类Dev

如何将命令输出到文件,而不会出现错误的空白文件?

来自分类Dev

将命令行输出导出到Python中的文件

来自分类Dev

将python生成的输出导出到excel文件

来自分类Dev

将ostringstream输出到文件

来自分类Dev

将结果输出到文件

来自分类Dev

将随机样本从CSV文件导出到新的CSV文件-输出混乱

来自分类Dev

Python3-编写循环输出到文件

来自分类Dev

输出到python 3中的文件的语法错误

来自分类Dev

Python将CSV数据导出到文件中

来自分类Dev

将Bundle Exec Rails C的输出结果输出到文件/ csv?

来自分类Dev

将CSV中的两列一起添加并输出到新的CSV文件

来自分类Dev

编辑CSV文件Python 3-将文件内容与打印输出匹配

来自分类Dev

将数据文件追加到CSV文件并输出到另一个CSV文件

来自分类Dev

Python :(将输出写入CSV)

Related 相关文章

热门标签

归档