调用耗时函数时用于循环流的Python

调用耗时函数时用于循环流的Python,python,function,python-2.7,for-loop,Python,Function,Python 2.7,For Loop,我已经实现了以下代码: lines=[] with open('path_to_file', 'r+') as source: for line in source: line = line.replace('\n','').strip() if line.split()[-1] != 'sent': # do some operation on line without 'sent' tag

我已经实现了以下代码:

lines=[]
    with open('path_to_file', 'r+') as source:
        for line in source:
            line = line.replace('\n','').strip()
            if line.split()[-1] != 'sent':
                # do some operation on line without 'sent' tag
                upload(data1.zip)
                upload(data2.zip) 
                do_operation(line)
                # tag the line
                line += '\tsent'
            line += '\n'
            # temporary save lines in a list
            lines.append(line)
        # move position to start of the file
        source.seek(0)
        # write back lines to the file
        source.writelines(lines)
我在
部分调用upload方法#对不带sent标签的行执行一些操作
,将数据上传到云。由于数据有点大(大约1GB),因此需要一段时间才能完成上传。同时,for循环是否继续调用
upload(data2)
?我收到错误,因为我无法同时上传

如果是,我如何避免这种情况

编辑:::


我已更改上载功能,以返回上载后的状态。那么,如何修改我的主循环,使其在调用
upload(data1.zip)
后等待,然后转到
upload(data2.zip)
。我想同步

您可以将它们作为独立进程发送。使用Python模块;那里也有不错的

您的内部循环可能如下所示:

up1 = Process(target=upload, args=(data1.zip,))
up2 = Process(target=upload, args=(data2.zip,))
up1.start()
up2.start()

# Now, do other stuff while these run
do_operation(line)
# tag the line
line += '\tsent'

# Wait for the uploads to finish -- in case they're slower than do_operation.
up1.join()
up2.join()


@是的,是我困惑了。。我想同步

优秀的;我们已经解决了。您同步的内容是独立的进程。您的主进程正在等待子进程的结果,即上载。多个进程称为…:-)


我们现在正处在解决问题的关键时刻吗?我认为您需要的部分包含在这些答案中的一个(最多两个)中。

您可以将它们作为独立的流程发送出去。使用Python模块;那里也有不错的

您的内部循环可能如下所示:

up1 = Process(target=upload, args=(data1.zip,))
up2 = Process(target=upload, args=(data2.zip,))
up1.start()
up2.start()

# Now, do other stuff while these run
do_operation(line)
# tag the line
line += '\tsent'

# Wait for the uploads to finish -- in case they're slower than do_operation.
up1.join()
up2.join()


@是的,是我困惑了。。我想同步

优秀的;我们已经解决了。您同步的内容是独立的进程。您的主进程正在等待子进程的结果,即上载。多个进程称为…:-)


我们现在正处在解决问题的关键时刻吗?我认为您需要的部分在这些答案中的一个(或最多两个)中。

您可以使用
多处理
完成耗时的工作

import multiprocessing


# creates processes for your files, each file has its own processor
processes = [multiprocessing.Process(target=upload, args=(zip_file,)) for zip_file in [data1.zip,data2.zip]]

# starts the processes
for p in processes:
    p.start()

# waits for all processes finish work
for p in processes:
    p.join()

# It will not go here until all files finish uploading.
...
...

您可以使用
多处理
完成耗时的工作

import multiprocessing


# creates processes for your files, each file has its own processor
processes = [multiprocessing.Process(target=upload, args=(zip_file,)) for zip_file in [data1.zip,data2.zip]]

# starts the processes
for p in processes:
    p.start()

# waits for all processes finish work
for p in processes:
    p.join()

# It will not go here until all files finish uploading.
...
...

我认为您的问题可能是您不想一次上载多个文件

您的代码不会尝试执行任何并行上载。因此,我怀疑您的
upload()
函数正在启动上载过程,然后让它在返回给您时在后台运行

如果这是真的,您可以尝试以下一些选项:

  • 将一个选项传递给
    upload
    函数,告诉它在返回之前等待上传完成

  • 发现(研究)可用于将程序与
    上载
    功能启动的进程同步的某些属性。例如,如果函数返回子进程id,则可以对该pid执行
    等待
    ,以完成。或者它会将pid写入一个pid文件——您可以读入数字,然后等待它

  • <> LI>

    如果你不能让上传函数同步地做你想做的事情,你可以考虑用“打印语句”来替换调用“代码>上载())/>代码>以使你的代码生成某种脚本,这些脚本可以单独执行,可能使用不同的环境或使用不同的上传工具。


    我认为您的问题可能是您不想一次上载多个文件

    您的代码不会尝试执行任何并行上载。因此,我怀疑您的
    upload()
    函数正在启动上载过程,然后让它在返回给您时在后台运行

    如果这是真的,您可以尝试以下一些选项:

  • 将一个选项传递给
    upload
    函数,告诉它在返回之前等待上传完成

  • 发现(研究)可用于将程序与
    上载
    功能启动的进程同步的某些属性。例如,如果函数返回子进程id,则可以对该pid执行
    等待
    ,以完成。或者它会将pid写入一个pid文件——您可以读入数字,然后等待它

  • <> LI>

    如果你不能让上传函数同步地做你想做的事情,你可以考虑用“打印语句”来替换调用“代码>上载())/>代码>以使你的代码生成某种脚本,这些脚本可以单独执行,可能使用不同的环境或使用不同的上传工具。


    上传完成后,我已编辑上传功能以返回(状态)。。如何要求循环等待返回值..抱歉,基本问题是..python新手如果上载函数正在等待上载完成,那么您应该可以开始了。如果并行上载仍有问题,是否可以编辑问题以包含
    upload()
    函数的源代码?我已编辑上载函数,以在上载完成后返回(状态)。。如何要求循环等待返回值..抱歉,基本问题是..python新手如果上载函数正在等待上载完成,那么您应该可以开始了。如果并行上传仍然存在问题,能否编辑问题以包含
    upload()
    函数的源代码?我已经编辑了我的问题。没有多个步骤怎么能做到呢?我已经编辑了我的问题。没有多个过程怎么能做到呢?我的问题稍微修改了一下。。如果没有MILTIProcessing,我怎么能做呢?我的问题稍微修改了一下。。没有Miltiprocessing我怎么能做到呢?我们中的一个人很困惑(是的,可能还是我)。您希望等待返回值,但不希望进行多处理。并行处理就是多处理。等待结果的是同步——这是上面的连接函数。你能澄清一下你对我们的需求吗?@Prune是的,我很困惑。。我想同步..我们中的一个很困惑(是的,可能还是我)。您想等待返回值,但是