多线程无法同时执行任务

时间:2019-06-04 11:57:55

标签: python python-3.x python-multithreading

我为我的项目编写了python代码,该文件将文件的文件名存储在列表中的SFTP服务器上,并对本地目录执行相同的操作,比较两个列表,然后下载不常见的文件。代码从格式为

的单独文本文件中获取服务器凭据

文本文件的内容(config9.txt)

127.0.0.1,username,password,log22.txt,/new45,C:\Users\udit\Desktop\ftp2\bob16

我的程序针对不同的IP逐一执行。我希望它使用多线程为所有IP并行运行它。我修改了我的代码,但仍然可以一一执行。有什么错误吗?

import os
import pysftp
import csv
import socket
from stat import S_IMODE, S_ISDIR, S_ISREG
import time
import threading
from threading import Thread
from time import sleep
import os.path
import shutil
import fileinput
import lock

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

def mainprocess():
    with open("config9.txt", "r") as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        for row in csv_reader:
            print(row)
            IP=row[0]
            myUsername=row[1]
            myPassword=row[2]
            txtfile=row[3]
            remotepath=row[4]
            localpath=row[5]

            with pysftp.Connection(host=IP, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
                r = str(socket.gethostbyaddr(IP))
                print("connection successful with " + r)


                def get_file2(sftp, remotedir):
                    result = []
                    for entry in sftp.listdir_attr(remotedir):
                        remotepath = remotedir + "/" + entry.filename
                        mode = entry.st_mode
                        if S_ISDIR(mode):
                            result += get_file2(sftp, remotepath)
                        elif S_ISREG(mode):
                            if (time.time() - entry.st_mtime) // (24 * 3600) > 0:
                                result.append(entry.filename)

                    return result


                remote_path = remotepath
                d = get_file2(sftp, remote_path)


                def process():
                    myName = []
                    with open(filename, 'r+') as f:
                        for name in f.readlines():
                            while '\n' in name:
                                name = name.replace('\n', '')
                                myName.append(name)
                    print(myName)
                    print(len(myName))

                    import os

                    filtered_list = [string for string in d if string not in myName]
                    print("filtered list:", filtered_list)
                    print(len(filtered_list))

                    local_path =localpath

                    def compare_files(sftp, remotedir, remotefile, localdir, preserve_mtime=True):
                        remotepath = remotedir + "/" + remotefile
                        localpath = os.path.join(localdir, remotefile)
                        mode = sftp.stat(remotepath).st_mode
                        if S_ISDIR(mode):
                            try:
                                os.mkdir(localpath, mode=777)
                            except OSError:
                                pass
                            compare_files(sftp, remotepath, localpath, preserve_mtime)
                        elif S_ISREG(mode):
                            sftp.get(remotepath, localpath, preserve_mtime=True)

                    for file in filtered_list:
                        compare_files(sftp, remote_path, file, local_path, preserve_mtime=False)

                    with open(filename, 'a') as f:
                        for item in filtered_list:
                            f.write("%s\n" % item)


                filename=txtfile
                try:
                    file = open(filename, 'r')
                    process()
                except IOError:
                    file = open(filename, 'w')
                    process()



t=time.time()

t1=threading.Thread(target=mainprocess(),args=())
t1.start()

t1.join()

0 个答案:

没有答案