Python Forum
running 2 loops at the same time - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: running 2 loops at the same time (/thread-25144.html)



running 2 loops at the same time - julio2000 - Mar-21-2020

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()



RE: running 2 loops at the same time - ndc85430 - Mar-21-2020

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.


RE: running 2 loops at the same time - julio2000 - Mar-21-2020

So the fix is this?
Thread(target=set_discord_status).start()
Thread(target=loop).start()



RE: running 2 loops at the same time - ndc85430 - Mar-21-2020

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.


RE: running 2 loops at the same time - julio2000 - Mar-21-2020

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...


RE: running 2 loops at the same time - ndc85430 - Mar-21-2020

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



RE: running 2 loops at the same time - julio2000 - Mar-21-2020

Idk how, but i fixed it haha. Thanks!


RE: running 2 loops at the same time - ndc85430 - Mar-21-2020

What did you change?