Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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中的多线程和数组_Python_Arrays_Multithreading_Scripting - Fatal编程技术网

Python中的多线程和数组

Python中的多线程和数组,python,arrays,multithreading,scripting,Python,Arrays,Multithreading,Scripting,我目前正在对现有脚本进行一些更改,但问题是,我没有多线程的经验,看到的示例越多,我就越感到困惑 以下是我试图实现的一点见解 脚本获得2个输入,即服务和操作,服务可以使用*查找所有正在运行的服务,这会使服务成为存储在数组中的列表 好的方面是,有一个基于服务和操作的函数,它执行一些操作,即 perform_service_action(service1, stop) 这将导致service1停止,以此类推 不幸的是,这个过程被配置为按顺序运行,脚本处理时间太长,所以我想知道的是,你们中是否有人对我

我目前正在对现有脚本进行一些更改,但问题是,我没有多线程的经验,看到的示例越多,我就越感到困惑

以下是我试图实现的一点见解

脚本获得2个输入,即服务和操作,服务可以使用
*
查找所有正在运行的服务,这会使服务成为存储在数组中的列表

好的方面是,有一个基于服务和操作的函数,它执行一些操作,即

perform_service_action(service1, stop)
这将导致service1停止,以此类推

不幸的是,这个过程被配置为按顺序运行,脚本处理时间太长,所以我想知道的是,你们中是否有人对我应该如何处理这个问题有什么建议,下面是代码的一小部分:

def main(argv):
    if len(argv) == 1:
        if argv[0] == 'list':
            list_of_serv = get_list(None)
            for service in list_of_serv:
                if not service == "":
                    print service
        else:
            help()
    elif len(argv) == 2:
        service_arg = argv[0]
        action = argv[1]
        list_of_serv = get_list(service_arg)
        for service in list_of_serv:
                perform_service_action(service, action)
        print_service_output()
    else:
        help()
我在这里和那里尝试了一些东西,但是我尝试得越多,我就越对线程主题感到困惑。这是将代码的主要部分更改为在具有不同值的相同函数的线程中运行,还是从头开始更改函数,即获取列表的函数和对服务执行操作的函数

我希望任何人都能在这方面提出一些建议


提前感谢。

对于I/O繁重的操作,您最好选择
多线程
,对于CPU繁重的操作,您最好选择
多处理
。因此,根据
perform\u service\u action
所做的工作,选择其中一项

由于您的问题没有明确说明操作的类型,因此我将假定其i/O负担沉重。Python
gevents
内部是我的并发goto库

main()
中,更改以下内容:

for service in list_of_serv:
            perform_service_action(service, action)

这将产生多个线程(确切地说是
greenlets
)。同时执行,

gevent.joinall(jobs, timeout=2)
您可以使用以下方式访问每个
作业的结果:

[job.value for job in jobs]

**请注意,生成greenlet期间的参数需要在
perform\u service\u action
方法中解包。(您可以使用*args或*kwargs,具体取决于您希望如何实现它)

您可以将代码更改为

 for service in list_of_serv:

    perform_service_action(service, action)
将是:

 from multiprocessing import Pool
 from multiprocessing.dummy import Pool as ThreadPool

 pool.map(perform_service_action, zip( [service],[action]*len(service) ))

 pool.close()
 pool.join()
您可以创建映射到函数的池或进程

  perform_service_action
然后将iterable(列表)传递给要处理的函数

如果需要函数的结果,也可以

result = pool.map(perform_service_action, zip( [service],[action]*len(service) ))

print (result ) 
这将为您提供所有结果的列表


这应该有助于提高速度,但这也取决于是否有所有的用户输入或只是数据

因此,在最后,我继续使用多线程而不是多处理进行更改,解决方案如下所示:

import threading



# For parallel execution
thread_limiter = threading.BoundedSemaphore(5)
thread_lock = threading.Lock()
thread_list = []

def main(argv):
if len(argv) == 1:
    if argv[0] == 'list':
        list_of_serv = get_list(None)
        for service in list_of_serv:
            if not service == "":
                print service
    else:
        help()
elif len(argv) == 2:
    service_arg = argv[0]
    action = argv[1]
    list_of_serv = get_list(service_arg)
    for service in list_of_serv:
        thread_limiter.acquire()
        thread = threading.Thread(target=perform_service_action, args=(service, action))
        thread.start()
        thread_list.append(thread)
        thread_limiter.release()
    [thread.join() for thread in thread_list]
    print_service_output()
else:
    help()
老实说,我把事情复杂化了,但对于任何有类似问题的人,请随便用这个例子


感谢并问候在这篇文章中帮助我的每一个人。

这些函数中发生了什么?它是I/O重还是CPU重?数组和列表在Python中有非常特殊的含义,我不确定您在整个过程中是否正确使用了它们。基本上,它只是驻留在服务器中的服务名称的列表,它只是第一个输入函数perform_service_action的值。它也是I/O密集型的,基本上是启动的,停止或重新启动服务,读取pid和正常运行时间,以定义的格式记录并打印结果。i、 e.馈送service1 stop将向service1发出停止请求,读取其当前pid和正常运行时间,打印服务停止后的停止状态,并显示如下内容:service pid status uptime service1 1111 stopped 00:00:00如果我们馈送*到脚本,这将获得服务器中现有服务的列表,并将其提供给服务的列表。因此,这个解决方案的问题是,这里唯一的数组是服务,它可以使用zip传递给函数,但要发送完全相同的值,它不应该在zip中,对吗。。。我这样问是因为我对Python完全陌生,仍然不习惯语法,每次我尝试代码时,它都会抛出一个错误,指出我只传递了1个参数,而不是它所需要的2个参数。@IsaelMacias我必须列出相同的字符串,因为它是必须放入函数中的参数的一部分。如果参数始终相同,则可以将变量放在其他位置,如全局变量。在这种情况下,如果您只传递一个变量,那么它只需要是一个iterable(列表就是其中之一),您已经有了它,因此不需要zip。
import threading



# For parallel execution
thread_limiter = threading.BoundedSemaphore(5)
thread_lock = threading.Lock()
thread_list = []

def main(argv):
if len(argv) == 1:
    if argv[0] == 'list':
        list_of_serv = get_list(None)
        for service in list_of_serv:
            if not service == "":
                print service
    else:
        help()
elif len(argv) == 2:
    service_arg = argv[0]
    action = argv[1]
    list_of_serv = get_list(service_arg)
    for service in list_of_serv:
        thread_limiter.acquire()
        thread = threading.Thread(target=perform_service_action, args=(service, action))
        thread.start()
        thread_list.append(thread)
        thread_limiter.release()
    [thread.join() for thread in thread_list]
    print_service_output()
else:
    help()