Python overgiving variable from one thread to the other multithreading -
how give variable _while.py __scheduler.py in multithreading szenario? tried stuff nothing seems working.
thread.py
from multiprocessing import process import _while import _scheduler if __name__ == '__main__': p1 = process(target=_while.main) p1.start() p2 = process(target=_scheduler.main) p2.start()
_while.py
import time def main(): while true: print "while" time.sleep(0.5) """getting button status""" """giving button status _scheudler._scheduler"""
__scheduler.py
import logging import time apscheduler.scheduler import scheduler _while import """button status""" def _scheduler(): print "scheduler" while """button status"""==true: print "button pressed" time.sleep(0.5) def main(): logging.basicconfig() scheduler = scheduler(standalone=true) scheduler.add_interval_job(_scheduler, seconds=2) scheduler.start() if __name__ == '__main__': main()
solution:
thread.py
from multiprocessing import process, value, array import time import _while import _scheduler if __name__ == '__main__': num = value('d', 0.0) arr = array('i', range(10)) p1 = process(target=_while.main, args=(num, arr)) p1.start() p2 = process(target=_scheduler.main, args=(num, arr)) p2.start() p1.join() p2.join() print num.value
_while
import time def main(num, arr): while true: print "while" num.value = 1 time.sleep(10) """getting button status""" """giving button status _scheudler._scheduler"""
__scheduler.py
import logging apscheduler.scheduler import scheduler def _scheduler(num, arr): while true: print num.value if num.value == 1: print "mongo" num.value = 0 break def main(num, arr): logging.basicconfig() scheduler = scheduler(standalone=true) scheduler.add_interval_job(_scheduler, args=(num, arr), seconds=2) scheduler.start() if __name__ == '__main__': main()
the problem left can't use value without using array
create instance of multiprocessing.value in threads.py before create p1 , p2, pass instance of value args both p1 , p2 , change main() method of _while.py , _scheduler.py accept new value parameter.
similar how done here http://docs.python.org/2/library/multiprocessing.html#sharing-state-between-processes
you use queues or pipes suggested euegene c. example can found here http://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes
Comments
Post a Comment