Python Forum
Alternative for time.sleep()?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alternative for time.sleep()?
#1
Ok, so I'm trying to design a giveaway system for an IRC chatbot. What I want to do is when someone puts in the !giveaway command, it sets isOpen to true for a few minutes (10sec here for testing), so people can do !enter and have their name added to the giveaway list.
My problem is that when I do sleep(), it pauses the code, so it doesn't respond when anyone does !enter until the giveaway is already over.

Are there any alternatives that fit my needs? Or maybe other ways to accomplish this?

Here's my code for the !giveaway and !enter commands.

elif "!giveaway" in str(message):
                def findWinner():
                    if len(giveaway) == 0:
                        utils.send(s, "The giveaway has ended with no entries.")
                    else:
                        winningNumber = random.randint(0, len(giveaway))
                        print (giveaway)
                        winningUser = giveaway[winningNumber]
                        print ("Winning Number is "+winningNumber+" and Winning User is "+winningUser)
                        utils.send(s, winningUser+", you've won the giveaway! Look in your PMs for additional info.")
                        utils.send(s, "/w "+winningUser+" This is an automated message. If you just won a sub giveaway, you should recieve your reward shortly. Congratulations again!")

                def giveawayCom():
                    giveaway = []
                    utils.send(s, "A giveaway has just begun! For the next 3 minutes, do !enter to put in your ticket!")
                    isOpen = True
                    sleep(10)
                    isOpen = False
                    utils.send(s, "The giveaway is closed! You may no longer enter.")
                    sleep(1)
                    utils.send(s, "/me is rolling the dice...")
                    findWinner()
                giveawayCom()
            elif "!enter" in str(message):
                if isOpen:
                    if username in giveaway:
                        utils.send(s, username+", you've already entered this giveaway!")
                    else:
                        giveaway.append(username)
                else:
                    utils.send(s, "There is no giveaway currently happening!")
Reply
#2
create a separate thread for the counter.
https://pymotw.com/2/threading/
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a difference between Python’s time.sleep and win32api.Sleep? phpjunkie 4 1,070 Sep-21-2024, 05:17 PM
Last Post: aakritiintelligence
  Use subprocess.Popen and time.sleep chucky831 2 2,948 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 5,203 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Time.sleep: stop appending item to the list if time is early quest 0 2,379 Apr-13-2021, 11:44 AM
Last Post: quest
  Can you end the Time.sleep function boier96 9 16,846 Jan-16-2021, 10:09 PM
Last Post: Serafim
  time.sleep mtnwinds 4 4,382 May-21-2020, 10:12 AM
Last Post: Larz60+
  kill thread or process asap, even during time.sleep nanok66 4 4,282 Apr-29-2020, 10:13 AM
Last Post: nanok66
  time.sleep works erratically, a bug in Python? stipcevic 2 5,536 Jan-21-2020, 09:38 PM
Last Post: Marbelous
  Why does time.sleep(wait) add time in python3 ? gerardg 4 4,451 Apr-28-2019, 06:26 PM
Last Post: gerardg
  How to achieve close to 1ms time.sleep kwekey 1 20,402 Mar-25-2019, 02:52 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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