Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 3rd, 2004, 09:26 AM
Johnny5ive Johnny5ive is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 18 Johnny5ive User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 53 m 27 sec
Reputation Power: 0
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)
Code:
def mainLoop():
    global listTime
    global fileTime
    global xPyGetPower
    listTimer = listTime
    fileTimer = fileTime
    while xPyGetPower == 'on':
        if requestLists == 'on':
            if listAvailable:
                if listTimer == 1:
                    request_list()
                    listTimer = listTime
                else:
                    listTimer = listTimer-1
        if requestFiles == 'on':
            if fileAvailable:
                if fileTimer == 1:
                    request_file()
                    fileTimer = fileTime
                else:
                    fileTimer = fileTimer-1
        sleep(2)



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 09:28 AM.

Reply With Quote
  #2  
Old April 3rd, 2004, 10:48 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,221 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 22 h 21 m 4 sec
Reputation Power: 263
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:

Code:
import threading

thread = threading.Thread(target=mainloop)
thread.start()
#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 http://heather.cs.ucdavis.edu/~matl...n/PyThreads.pdf.

Hope this helps

Dave - The Developers' Coach

Reply With Quote
  #3  
Old April 4th, 2004, 01:44 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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.

Reply With Quote
  #4  
Old April 4th, 2004, 03:31 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
I think this might be what your looking for:
Quote:
xchat.hook_timer(timeout, callback, userdata=None)
This function allows you to register a callback to be called every timeout milliseconds.
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python Loops...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT