Python Forum
running 2 loops at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
running 2 loops at the same time
#1
Hey,
I've created my own program, and i want my program's user's discord, to be telling that he is 'playing' my program while he is running it. I've created a function (set_discord_status) wich sets the user's discord status to: 'playing Storm Aio'. But this is a loop.. And i want this loop to run at the same time as my program. I've created a other loop (loop) wich demonstrates my program. And so, I want my program (loop - in this case) to run at the same time as the loop wich sets my discord status. i've created the code below but when I run that. It will set the discord status, but it won't run the other loop...

Does someone know what i did wrong?

from threading import Thread
import rpc
import time


def set_discord_status(): # This is a function wich sets my discord status to a specific game
    client_id = '689533785343000590'
    rpc_obj = rpc.DiscordIpcClient.for_platform(client_id)

    start_time = time.time()
    while True:
        activity = {
                "state": "Beta version: 0.0.1",
                "details": "Taking stock...",
                "timestamps": {
                    "start": start_time
                },
                "assets": {
                    "small_text": "Light",
                    "small_image": "little",
                    "large_text": "StormAIO",
                    "large_image": "default"
                }
            }
        rpc_obj.set_activity(activity)


def loop(): # This is a loop just to test if they can run at the same time
    while True:
        print('hoi')


Thread(target=set_discord_status()).start()
Thread(target=loop()).start()
Reply
#2
Lines 33 and 34 are wrong. You're calling the functions there. The thing you pass as the target parameter should be the function itself (e.g. loop - note the lack of parens). This makes sense if you think about it - it's the job of the thread (its run method in particular) to call your function.
Reply
#3
So the fix is this?
Thread(target=set_discord_status).start()
Thread(target=loop).start()
Reply
#4
Yes. Did you try it?

It should be obvious why it wasn't working with the original code - you were calling set_discord_status on line 33, i.e. in the main thread and since it never completes (due to the infinite loop), execution never reached line 34. For that matter, the thread you created on line 33 was never started, either.
Reply
#5
yeah but how do i make sure execution makes it to line 34?

When I run it like this:
Thread(target=set_discord_status).start()
Thread(target=loop).start()
The set_discord_status function and its loop dont work...
Reply
#6
Define "don't work". I have no way to try and reproduce your issue, since I don't have Discord. Perhaps you can put some debugging print statements inside the function to see what's happening?

This simple example is doing as expected:

import threading
import time

def foo():
    i = 0
    while True:
        print(i)        
        i += 1
        time.sleep(0.1)

def bar():
    while True:
        print("Bar")
        time.sleep(0.5)


threading.Thread(target=foo).start()
threading.Thread(target=bar).start()
Sample output:

Output:
$ python3 thread.py 0 Bar 1 2 3 4 Bar 5 6 7 8 9 Bar
Reply
#7
Idk how, but i fixed it haha. Thanks!
Reply
#8
What did you change?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Running an action only if time condition is met alexbca 5 1,261 Oct-27-2022, 02:15 PM
Last Post: alexbca
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,128 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Assistance with running a few lines of code at an EXACT time nethatar 5 3,165 Feb-24-2021, 10:43 PM
Last Post: nilamo
  Running multiple script at the same time LoganSulpizio 1 6,920 Dec-07-2019, 04:30 PM
Last Post: Clunk_Head
  Error when running the second time metro17 3 3,699 Aug-30-2019, 12:09 PM
Last Post: ThomasL
  Time based loops Owenix 4 3,682 Sep-20-2018, 05:37 PM
Last Post: Owenix
  Multi-processing - problem with running multiple *.py files at the same time Antonio 5 3,745 Sep-12-2018, 01:08 PM
Last Post: volcano63
  Running Date and Time in SQLITE using Python trabis03 3 4,270 Jul-17-2017, 02:10 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