【问题标题】:INSERT into a DB table the results from downloaded files using Pysftp and Multithreading使用 Pysftp 和多线程将下载文件的结果插入数据库表
【发布时间】:2018-05-08 15:32:33
【问题描述】:

仍然与以下问题有关... Parallel downloads with Multiprocessing and PySftp

我想知道如何打印成功的下载?我的意图实际上是在数据库表中附加一条记录,以便创建带有文件名、日期和时间的下载文件日志。

有什么想法吗?我搜索了一些示例并进行了一些测试,但似乎我的下载模块无法返回任何内容,或者我没有使用正确的代码来读取结果并打印出来。

下载功能

import pysftp
import os

def fdownload(vfileaux):

    vtmpspl = vfileaux.split(',')

    vfile = vtmpspl[0]
    vhost = vtmpspl[1]
    vlogin = vtmpspl[2]
    vpwd = vtmpspl[3]
    vftppath = vtmpspl[4]
    vlocalpath = vtmpspl[5]

    os.chdir(vlocalpath)
    os.getcwd()

    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None

    vfilecheck = vlocalpath + '/' + vfile

    if not os.path.isfile(vfilecheck):

        vftpaux = pysftp.Connection(host=vhost, username=vlogin, password=vpwd, cnopts=cnopts)
        vftpaux.cwd(vftppath)
        vftpaux.get(vfile, preserve_mtime=True)
        vftpaux.close()

        return vnename + "_" + vdatetime

    else:
        pass

主要功能

from datetime import *
from ffilelist import *
from ffilefilter import *
from developing.fdownload import *
import pymysql.cursors
from concurrent.futures import ThreadPoolExecutor, wait, as_completed

def main():

    print(datetime.datetime.now(), 'Loading variables...')

    vhostlist = {}
    vloginlist = {}
    vpwdlist = {}
    vftppathlist = {}
    vlocalpathlist = {}

    vhostaux = '10.11.12.13'
    vhostlist[vhostaux] = vhostaux
    vloginlist[vhostaux] = 'admin'
    vpwdlist[vhostaux] = 'pass1234'
    vftppathlist[vhostaux] = '/export/home'
    vlocalpathlist[vhostaux] = 'd:/test/'

    vfilelist1 = []

    global vfilelist2
    vfilelist2 = []

    for vhosttmp in vhostlist:

        print(datetime.datetime.now(), 'Starting to process ' + vhosttmp + "...")

        global vhost
        global vlogin
        global vpwd
        global vftppath
        global vlocalpath

        vhost = vhostlist[vhosttmp]
        vlogin = vloginlist[vhosttmp]
        vpwd = vpwdlist[vhosttmp]
        vftppath = vftppathlist[vhosttmp]
        vlocalpath = vlocalpathlist[vhosttmp]

        vfilelist1 = ffilelist(vhost, vlogin, vpwd, vftppath)

        print(datetime.datetime.now(), 'Vectorizing download file     list...')

        for vfile in vfilelist1:
            vfilelist2.append(vfile + ',' + vhost + ',' + vlogin + ',' +     vpwd + ',' + vftppath + ',' + vlocalpath)

    vfilelist0 = ffilefilter(vfilelist2)

    print(datetime.datetime.now(), 'Starting simultaneous downloads...')

    vpool = concurrent.futures.ProcessPoolExecutor(max_workers=8)
    vpool.map(fdownload, vfilelist0)
    vpool.shutdown()

    print(datetime.datetime.now(), 'Downloads finished!')

要存储在 MARIADB 中的日志的 INSERT 字符串是这样的。已经测试和工作。一旦我找到了获取下载文件列表的解决方案,就可以在 MAIN 函数中使用。

vconn = pymysql.connect(host='localhost', user='root', password='pass1234', db='test')
vcurs = vconn.cursor()
vsql = "INSERT INTO `logs_download` (`ne`, `datetime`) VALUES (\'" + vnename + "\', \'" + vdatetime + "\')"
vcurs.execute(vsql)
vconn.commit()

【问题讨论】:

  • 如果您尝试从 fdownload 中的 return vnename + "_" + vdatetime 检索值,它们将在返回列表的 vpool.map(fdownload, vfilelist0) 的结果中。试试print(vpool.map(fdownload, vfilelist0))

标签: python multithreading threadpoolexecutor pysftp


【解决方案1】:

我已经尝试过 Alex 的建议...所以我更改了部分代码:

vpool = concurrent.futures.ThreadPoolExecutor(max_workers=8) 
print(vpool.map(fdownload, vfilelist0)) 
vpool.shutdown()

...但是得到了以下结果:

2018-05-08 12:44:25.115066 Loading variables...
2018-05-08 12:44:25.115066 Starting to process 10.11.12.13...
2018-05-08 12:44:25.115066 Disabling known hosts...
2018-05-08 12:44:25.115066 Opening FTP connection...
2018-05-08 12:44:26.567149 Reading objects list...
2018-05-08 12:45:30.580015 Separating files from folders...
2018-05-08 12:45:30.584015 Closing FTP connection...
2018-05-08 12:45:30.585015 Vectorizing download file list...
2018-05-08 12:45:30.596016 Filtering latest file for each object...
2018-05-08 12:45:30.648019 Vectorizing only latest files...
2018-05-08 12:45:30.652019 Starting simultaneous downloads...
<generator object Executor.map.<locals>.result_iterator at 0x04332D20>

【讨论】:

  • 抱歉,我以为这是一个列表。您可以轻松转换:print(list(vpool.map(fdownload, vfilelist0)))
猜你喜欢
  • 2017-11-21
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 2019-03-14
相关资源
最近更新 更多