并行运行两个函数

时间:2019-05-02 15:52:54

标签: python pathos

我有2种不同的功能,比如说:

word = str(input("your word: "))
print(word)
run = True
while run:
    #ensure he enters a number not letter 
    while True:
        try:
            get_index = int(input("enter index: "))
            break
        except:
            print("Character must be a lowercase letter!")
    #check to see if the index is within the provided word lenght
    while -1 < get_index < len(word):
        #doesn't matter if he enters an uppercase letter becasue the lowermethod will turn it to lowercase
        get_letter = str(input("enter letter: ")).lower()
        #check to ensure he provides one letter only
        while len(get_letter) == 1:
            word = word[:get_index] + get_letter + word[get_index + 1 :]
            print(word)
            break
        else:
            print("Must be exactly one character!")
        break
    else:
        #quits if the index is -1
        if get_index == -1:
            run = False
        #if the index not -1 not in the length of the word ,prints invalid
        else:
            print("Invalid index")

我想同时在悲痛中同时运行它们。

在多处理中,我将执行以下操作:

def foo(print_me):   
    print(print_me + " foo")

def foo2(print_me):   
    print(print_me + " foo2")

我该如何在悲伤中做类似的事情?

P.S
我不能使用python多处理,因为在实际函数中,我使用的process = [Process(target=foo, args=("HI")),Process(target=foo2, args=("HI2")] map(lambda p: p.start(), process) map(lambda p: p.join(), process) 不能使用多处理(由于泡菜错误)。

1 个答案:

答案 0 :(得分:1)

赞:

>>> def foo(print_me):   
...     print(print_me + " foo")
... 
>>> def foo2(print_me):   
...     print(print_me + " foo2")
... 
>>> from pathos.helpers import mp
>>> process = [mp.Process(target=foo, args=("HI",)),mp.Process(target=foo2, args=("HI2",))]
>>> r1 = map(lambda p: p.start(), process) 
>>> r2 = map(lambda p: p.join(), process) 
>>> r1 = list(r1); r1 = list(r2)
HI foo
HI2 foo2
>>> 

我还纠正了您上面代码中的错别字,但没有纠正您的问题。