Thread: Python Loops...

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Rep Power

    Unhappy Python Loops...

    Hello all..

    I'm writing a script for xChat with Python. Currently, there is a loop I am creating that needs to happen every X seconds, and for the commands in that loop to happen in between. Not only that, but the other parts of the script have to also respond as triggered by the text in the channel. Here is what I have for the loop:

    (assuming the variables are set correctly, listTime is set @ 60, and fileTime is set @ 120)
    def mainLoop():
        global listTime
        global fileTime
        global xPyGetPower
        listTimer = listTime
        fileTimer = fileTime
        while xPyGetPower == 'on':
            if requestLists == 'on':
                if listAvailable:
                    if listTimer == 1:
                        listTimer = listTime
                        listTimer = listTimer-1
            if requestFiles == 'on':
                if fileAvailable:
                    if fileTimer == 1:
                        fileTimer = fileTime
                        fileTimer = fileTimer-1

    Here's the problem. I fire the baby up, and it just sits there. Oh it loads the script, but I can't do anything else, and the rest of the script doesn't respond.

    So how do I create a timed loop (every 2 seconds) that allows for the rest of the script to run at the same time (i'm assuming this is multithreading?) and doesn't lock up the program?!?

    Any help would be appreciated!
    Last edited by Johnny5ive; April 3rd, 2004 at 10:28 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    London, England
    Rep Power
    Yes, you will need to use threads to do what you want. This can be a scary, complex topic but the basics are quite simple.

    Instead of calling your mainloop function directly you can run it in a separate thread like this:

    import threading
    thread = threading.Thread(target=mainloop)
    #do the rest of your script...
    (I suggest renaming the 'mainloop' function, since it is no longer part of the main thread)

    The thread will end when the function terminates.

    The main problem to watch out for when using threads is having two threads access the same objects at the same time. There are various ways round this, which are documented in the threading module.

    A quick search on google found this tutorial on threading in Python at

    Hope this helps

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Houston, TX
    Rep Power
    I'm not so sure that threading a python plugin is a good idea from XChat ...
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Cambridge, UK
    Rep Power
    I think this might be what your looking for:
    xchat.hook_timer(timeout, callback, userdata=None)
    This function allows you to register a callback to be called every timeout milliseconds.

IMN logo majestic logo threadwatch logo seochat tools logo