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
  Twilio alternative jmair 3 3,886 Feb-08-2024, 01:55 PM
Last Post: Sharmi
  Pillow alternative? kucingkembar 4 864 Jul-27-2023, 10:50 AM
Last Post: Larz60+
  Use subprocess.Popen and time.sleep chucky831 2 1,942 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 3,519 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  Time.sleep: stop appending item to the list if time is early quest 0 1,870 Apr-13-2021, 11:44 AM
Last Post: quest
  Can you end the Time.sleep function boier96 9 9,419 Jan-16-2021, 10:09 PM
Last Post: Serafim
  Alternative for Cairosvg? Maryan 0 2,448 Oct-26-2020, 01:27 PM
Last Post: Maryan
  time.sleep mtnwinds 4 2,853 May-21-2020, 10:12 AM
Last Post: Larz60+
  kill thread or process asap, even during time.sleep nanok66 4 2,928 Apr-29-2020, 10:13 AM
Last Post: nanok66
  time.sleep works erratically, a bug in Python? stipcevic 2 3,881 Jan-21-2020, 09:38 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

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