Python Forum
Code issue with time remaining loop. Python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code issue with time remaining loop. Python3
#5
Ok, so after googling, the multiprocessing module itself has a version of Queue which is safe for... multiprocessing. So I made a few minor changes to use that queue instead, and also to how you're checking if there's a message in the queue (better to ask forgiveness than permission):

import time
import multiprocessing
import queue

def cleancycle(seconds):
    msgqueue=multiprocessing.Queue()
    ctr=multiprocessing.Process(target=timeremaining,args=(seconds,msgqueue))
    print('clean')
    print('Queue: {}'.format(queue))
    print('clean on')
    ctr.start()
    time.sleep(10)
    msgqueue.put('stop')

def timeremaining(seconds=0,msgqueue=None):
    print('Time: {}'.format(seconds))
    print('Queue: {}'.format(queue))
    while seconds != 0:
        print('time remaining: {}'.format(seconds))
        seconds-=1
        time.sleep(1)
        try:
            msg=msgqueue.get(False)
            if msg=='stop':
                print('Exit loop')
                break
        except queue.Empty:
            pass
def main():
    seconds=100
    while True:
        cleancycle(seconds)
        time.sleep(120)

if __name__ == '__main__':
    main()
Reply


Messages In This Thread
RE: Code issue with time remaining loop. Python3 - by nilamo - May-03-2017, 08:43 PM
queue not passing updated message - by deboerdn2000 - May-03-2017, 07:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating Code And Having Issue With Keys Xileron 8 1,293 May-25-2023, 11:14 PM
Last Post: DigiGod
  Python3 for loop over a dict ogautier 3 1,467 Feb-25-2022, 10:17 AM
Last Post: Larz60+
  NameError issue with daughter's newb code MrGonk 2 1,506 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Calculator code issue using list kirt6405 4 2,354 Jun-11-2021, 10:13 PM
Last Post: topfox
  How to measure execution time of a multithread loop spacedog 2 2,940 Apr-24-2021, 07:52 AM
Last Post: spacedog
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,382 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Stumped by my own code (ratio & epoch-time calculation). MvGulik 2 2,195 Dec-30-2020, 12:04 AM
Last Post: MvGulik
  Code taking too much time to process ErPipex 11 5,053 Nov-16-2020, 09:42 AM
Last Post: DeaD_EyE
  What is the run time complexity of this code and please explain? samlee916 2 2,341 Nov-06-2020, 02:37 PM
Last Post: deanhystad
  The count variable is giving me a hard time in this code D4isyy 2 2,014 Aug-09-2020, 10:32 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020