简体   繁体   English

python for循环中的多线程

[英]multithreading in python for loop

I have a for loop where I am passing list of schema names to the loop. 我有一个for循环,其中我将架构名称列表传递给了循环。 I want to execute these in parallel because there is no relation between these schemas. 我想并行执行这些,因为这些架构之间没有关系。

I am using Python 2.6. 我正在使用Python 2.6。

Creating dictionary here with schema information. 在此处使用架构信息创建字典。

region_by_schema = {}
region_by_schema['UTEST_TT'] = 'tt'
region_by_schema['UTEST_V3'] = 'v3'
region_by_schema['UTEST_WRITE_SERVER'] = ''
region_by_schema['UTEST_PROPERTY'] = ''
region_by_schema['UTEST_PROJECT'] = ''

Looping through schema here: 在这里遍历模式:

for base_schema in region_by_schema.keys():
    do_one_user(base_schema, opt_password)

In do_one_user I am creating a directory with base_schema name and I am doing some work there. do_one_user我正在创建一个具有base_schema名称的目录,并在那里做一些工作。

So I want to execute these 5 schemas in parallel so that my work will finish fast. 因此,我想并行执行这5个模式,以便我的工作能够快速完成。 I want to put the code below in threads or multiprocessing: 我想将下面的代码放入线程或多处理中:

for base_schema in region_by_schema.keys():
    do_one_user(base_schema, opt_password)

Please let me know the approach. 请让我知道该方法。

I suggest you read instruction of threading and learn how to use it. 我建议您阅读线程说明并学习如何使用它。 Here is link . 这是链接

from threading import Thread

yourSchemas = [schema1, schema2, schema3]
t = []
def test(region_by_schema):
    for base_schema in region_by_schema.keys():
        do_one_user(base_schema, opt_password)

    for schema in yourSchemas:
        t.append(Thread(target=test, args=[schema]))

    for i in range(len(yourSchemas)):
        t.start()
        t.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM