简体   繁体   English

需要并行运行相同的python文件

[英]Need of running the same python file in parallel

I have a python program running. 我正在运行一个python程序。 It is basically does an ssh connection and sustains that connection ie the program does not enter bash shell again until terminated manually. 它基本上是进行ssh连接并维持该连接,即程序直到手动终止后才再次进入bash shell。 Something like below, 像下面这样

bash-4.1$ python monitor.py
CONNECTION MADE
one done...
two done...
three done...
.
.
.

I want to same monitor.py to run in parallel for various such ssh connections. 我想让同一个monitor.py并行运行以进行各种ssh连接。 All of them are independent of each other and needn't exchange information. 它们都是彼此独立的,不需要交换信息。 Can I achieve this using by Multithreading or Multiprocessing ? 我可以通过Multithreading或多Multiprocessing吗?

You can make multiple threads to achieve what you have mentioned in your question. 您可以创建多个线程来实现您在问题中提到的内容。 Below code is an example of multithreading in Python. 以下代码是Python中的多线程示例。 Try it with your file. 尝试使用您的文件。

import thread
import time

# Define a function for the thread
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )

# Create two threads as follows
try:
   thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread.start_new_thread( print_time, ("Thread-2", 4, ) )
except:
   print "Error: unable to start thread"

while 1:
   pass

Output 输出量

Thread-1: Thu Jan 22 15:42:17 2009
Thread-1: Thu Jan 22 15:42:19 2009
Thread-2: Thu Jan 22 15:42:19 2009
Thread-1: Thu Jan 22 15:42:21 2009
Thread-2: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:23 2009
Thread-1: Thu Jan 22 15:42:25 2009
Thread-2: Thu Jan 22 15:42:27 2009
Thread-2: Thu Jan 22 15:42:31 2009
Thread-2: Thu Jan 22 15:42:35 2009

Here is a similar example, that uses multiprocessing instead of multithreading (for documentation, see the official docs ). 这是一个类似的示例,它使用多处理而不是多线程(有关文档,请参见官方文档 )。 Mulitprocessing works very similarly to multithreading, but it circumvents the Global Interpreter Lock, therefore allowing your script to actually run different processes at the same time, and potentially making better use of limited computing resources. Mulitprocessing的工作原理与多线程非常相似,但是它规避了全局解释器锁,因此允许您的脚本同时实际运行不同的进程,并有可能更好地利用有限的计算资源。

import multiprocessing as mp 将多处理导入为mp

def my_function(*args):
    print("Arguments: {0}".format(args))


class MyProcess(mp.Process):
    def __init__(self, target, args):
        mp.Process.__init__(self, target=target, args=args)

def main():
    a1 = MyProcess(target=my_function, args=("1st Process...",))
    a2 = MyProcess(target=my_function, args=("2nd Process...",))
    a3 = MyProcess(target=my_function, args=("3rd Process...",))
    a4 = MyProcess(target=my_function, args=("4th Process...",))

    proclist = [a1, a2, a3, a4]
    for proc in proclist:
        proc.start()

    for proc in proclist:
        proc.join()

if __name__ == '__main__':
    main()

Output: 输出:

Arguments: ('1st Process...',)
Arguments: ('2nd Process...',)
Arguments: ('3rd Process...',)
Arguments: ('4th Process...',)

While these came in in what appears to be a set order, if you add a task that takes a non-deterministic time, they will come in in the order they finish. 尽管这些任务似乎是固定的顺序,但是如果您添加的任务花费的时间不确定,它们将按照完成的顺序出现。 Just replace the contents of my_function with your code, and you should be set. 只需将my_function的内容替换为您的代码,就应该设置好了。 (Note: this uses Python 2. The same works in Python 3 with very little modification--maybe none--but if you're in Python 3, you should also investigate concurrent.futures ). (注意:这使用的是Python2。在Python 3中也可以使用相同的功能,只需进行很少的修改(也许没有做任何修改),但是如果您使用的是Python 3,则还应该研究concurrent.futures )。

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

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