Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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 ftp文件线程或多进程_Python - Fatal编程技术网

python ftp文件线程或多进程

python ftp文件线程或多进程,python,Python,我们不断地从数据库中生成数据文件,然后通过FTP发送到不同的位置。我们有一个poler,它会持续监视这个文件夹,一旦它看到文件,就会将其FTP 目前,此过程是按顺序进行的,当文件变大时,会进行备份,这会导致明显的延迟。我想在并行处理的事情,即FTP多个文件在同一时间。我不确定线程/多处理以及这对我来说是如何工作的 这是我的密码 import ftplib ftp = ftplib.FTP('domainname.com') ftp.login("username","password") inf

我们不断地从数据库中生成数据文件,然后通过FTP发送到不同的位置。我们有一个poler,它会持续监视这个文件夹,一旦它看到文件,就会将其FTP

目前,此过程是按顺序进行的,当文件变大时,会进行备份,这会导致明显的延迟。我想在并行处理的事情,即FTP多个文件在同一时间。我不确定线程/多处理以及这对我来说是如何工作的

这是我的密码

import ftplib
ftp = ftplib.FTP('domainname.com')
ftp.login("username","password")
infiles = [file1,file2,file3.....filen]
for x in infiles:
    f = open(x,'rb')
    ftp.storbinary('STOR %s' %x, f)
    f.close()
ftp.quit() 

我在想,既然这是I/O密集型的多处理方式,那么有没有关于如何进行的想法

将耗时的任务放入函数中

from multiprocessing import Process

def upload_file(filename):
    ftp = ftplib.FTP('domainname.com')
    ftp.login("username","password")
    f = open(x,'rb')
    ftp.storbinary('STOR %s' %x, f)
    f.close()
    ftp.quit() 

def multi_ftp():
    files = [file1, file2, ...]
    processes = []
    for filename in files:
        p = Process(target=filename, args=(filename,))
        p.start()
        processes.append(p)

    for p in processes:
        p.join()

将耗时的任务放入函数中

from multiprocessing import Process

def upload_file(filename):
    ftp = ftplib.FTP('domainname.com')
    ftp.login("username","password")
    f = open(x,'rb')
    ftp.storbinary('STOR %s' %x, f)
    f.close()
    ftp.quit() 

def multi_ftp():
    files = [file1, file2, ...]
    processes = []
    for filename in files:
        p = Process(target=filename, args=(filename,))
        p.start()
        processes.append(p)

    for p in processes:
        p.join()
您可能会发现作为更高级别的接口很有用

from multiprocessing import Pool
def upload(file): 
    ftp = ftplib.FTP('domainname.com')
    ftp.login("username","password")
    f = open(x,'rb')
    ftp.storbinary('STOR %s' %x, f)
    f.close()
    ftp.quit()


infiles = [file1,file2,file3.....filen]

pool = Pool(10) # submit 10 at once
pool.map(upload,infiles)
这很好,因为
map
的行为类似于内置函数。对于调试,只需更换
pool.map
-->
map

作为更高级别的接口,您可能会发现它很有用

from multiprocessing import Pool
def upload(file): 
    ftp = ftplib.FTP('domainname.com')
    ftp.login("username","password")
    f = open(x,'rb')
    ftp.storbinary('STOR %s' %x, f)
    f.close()
    ftp.quit()


infiles = [file1,file2,file3.....filen]

pool = Pool(10) # submit 10 at once
pool.map(upload,infiles)

这很好,因为
map
的行为类似于内置函数。对于调试,只需替换
pool.map
-->
map

我手头上没有代码片段,但对于生产环境,我会非常关注。Twisted不是最容易开始使用的工具,但它带来了很多东西,您稍后将自己实现。因此,我建议至少花几个小时来检查一下。

我手边没有代码片段,但对于生产环境,我肯定会看一下。Twisted不是最容易开始使用的工具,但它带来了很多东西,您稍后将自己实现。因此,我建议您至少花几个小时检查它。

如果它是I/O绑定的,那么您可以使用
多处理.pool.ThreadPool
(接口相同,唯一的更改是导入)。如果它是I/O绑定的,那么您可以使用
多处理.pool.ThreadPool
(接口相同,唯一的更改是导入)。