|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
I'm not so sure that threading a python plugin is a good idea from XChat ...
|
|
#4
|
||||
|
||||
|
I think this might be what your looking for:
Quote:
__________________
*** Experimental Python Markup CGI V2 *** |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python Loops... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|