0
import multiprocessing as mp . 
from multiprocessing import Process, Queue, Manager . 
from threading import Thread . 
from subprocess import Popen, PIPE, STDOUT . 
count = 0 
def loop1(): 
 while True:
 for i in range(0, 100): 
 count = count + 1 . 
 time.sleep(2)
 if count == 100:
 count = 0
def worker(filename): 
 proc = Popen(["{0}".format(filename)], stdout=PIPE, stderr=STDOUT, shell=True)
 (proc.communicate()[0])
def loop2():
 while True: 
 for i in ["a.py", "b.py"]: 
 if count == 50: 
 # run some executable in background and do not wait for it . 
 p = Process(target=worker, args=(i)) . 
 a.sleep(2) 
if __name__ == '__main__': 
 T1 = Thread(target=loop1, args=()) 
 T2 = Thread(target=loop2, args=()) 
 T1.start() . 
 T2.start() . 
 #T1.join() . 
 #T2.join()

1) how should I start two methods in parallel ? I need to check the variable status of method1 in method2? T1.start() and T2.start() starting one by one not at the same time.

2) the tasks of loop2 has to run again after 50 seconds.

asked Nov 13, 2018 at 17:44
2
  • Threading will not give you an exact start point for both threads. Reason for that is how python threading works and because of GIL (wiki.python.org/moin/GlobalInterpreterLock). Only option to do it (but it also will not be a perfect solution) is to use python multiprocessing and sync mechanism. Commented Nov 13, 2018 at 18:10
  • I tried to change it to : T1 =Process(target=loop1, args=()) T2 = Process(target=loop2, args=()) Commented Nov 13, 2018 at 18:13

1 Answer 1

1

You need to use Event to synchronize processes. With multiprocessing, both processes will be started in a separate python instance so it should give you pretty good timing. Take a look at below code:

from multiprocessing import Process, Event
import os
import time
def task(event, rerun=None):
 proc_id = os.getpid()
 event.wait() # Wait for an event
 print('timestamp of process id {}: {}'.format(proc_id, time.time()))
 if rerun is not None:
 time.sleep(rerun)
 print('timestamp of process id {}: {}'.format(proc_id, time.time()))
if __name__ == '__main__':
 e = Event() # Create event that will be used for synchronization
 p1 = Process(target=task, args=(e,))
 p1.start()
 
 # Start second task with rerun after 2 seconds
 p2 = Process(target=task, args=(e, 2))
 p2.start()
 e.set() # Set event so all processes can start at the same time

This will produce output like this:

timestamp of process id 28415: 1542278592.7580822
timestamp of process id 28416: 1542278592.7585154
timestamp of process id 28416: 1542278594.7604039

As for other things just play with this code

answered Nov 15, 2018 at 10:47
Sign up to request clarification or add additional context in comments.

1 Comment

In task, the variable e should be named event (see the arguments).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.