Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

Python How to schedule the parallel scripts for every seconds ? like (cron job)

Hey everyone i have script that works parallel, i was using APScheduler for scheduling the tasks but it works synchron (BlockingScheduler,BackgroundScheduler) doesnt work on parallel processes. What would be your advices , how can i run the parallel processes for every second ? also im using multiprocesses for parallel

ED?T:I have just solved it, if anyone gets trouble like this issue, here the example

from multiprocessing import Process

from apscheduler.schedulers.background import BlockingScheduler

def work_log_cpu1():
    print(" Proces work_log_cpu1")
    list11=[]
    for i in range(10000000):
        list11.append(i*2)
    print("Proces work_log_cpu1 finished")


def work_log_cpu2():
    print("Proces work_log_cpu2")
    list12=[]
    for i in range(10000000):
        list12.append(i*2)
    print("Proces work_log_cpu2 finished")


def work_log_cpu3():
    print(" Proces work_log_cpu3")
    list13=[]
    for i in range(10000000):
        list13.append(i*2)
    print("Proces work_log_cpu3 finished")
    


def main():
    # sleeps=[3,5,2,7]
    process=Process(target=work_log_cpu1)
    process2=Process(target=work_log_cpu2)
    process3=Process(target=work_log_cpu3)


    process.start()
    process2.start()
    process3.start()

    
    process.join()
    process2.join()
    process3.join()




if __name__ == '__main__':
    # main()
    sched.add_job(main, 'interval', seconds=1,id='first_job',max_instances=1)
    sched.start()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What's wrong with multiprocessing?

import multiprocessing

p1 = multiprocessing.Process(target=func1, args=("var1", "var2",))
p2 = multiprocessing.Process(target=func2, args=("var3", "var4",))
p1.start()
p2.start()
p2.join()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...