![]() |
Multitasking and GTK with python
hi everybody
im using python i have a problem about "while loops" but the problem is when i run gtk.main() function it is not allowing me to run another loop. :/ and if i run another loop it is stopping gtk window :/ def read(): global sock while 1: a=sock.recv(1024) if len(a)==0: break print a this is the function i need to run meanwhile gtk is running :/ please help i googled a lil bit but i couldnt find a proper tutorial on handling multitasking |
Re: Multitasking and GTK with python
Look for threads and threading modules in python docs.
Beware that you can only call gtk from the main thread. |
Re: Multitasking and GTK with python
class gui(threading.Thread):
__def __init__(self): ____...... ____..... ____self.window.show_all() ____threading.Thread.__İnit__(self) __def run(self): ____gtk.main() while 1: __print 'a' __gui().start() it supposed to print 'a' forever but after the gtk window showed up it stopped please help :/ |
Re: Multitasking and GTK with python
most of the tutorials on web are about "for loops" and handling them with threading module
but i guess gtk.main() function has a while loop in it |
Re: Multitasking and GTK with python
http://www.devshed.com/c/a/Python/Ba...g-in-Python/1/
http://www.wellho.net/solutions/pyth...t-example.html they are all about for loops http://stackoverflow.com/questions/8...hile-true-loop i found this but :/ i didnt get it |
Re: Multitasking and GTK with python
Quote:
Anyway, as you've being said earlier by maacruz, you need to verify that you call gtk.main() from the main thread only, and in the code example you posted, you are not doing that (run() is called by the child threads, therefore gtk.main() is being called by the child threads). EDIT: to get the expected behavior from the code, replace "print" with the "gtk.main()", and take "gtk.main()" outside the any class or loop (place at the bottom after gui.start()). EDIT2: There's really no difference between "for", "loop" and "while", all of them interpret to the same assembly calls, just with different handling for the conditions. |
Re: Multitasking and GTK with python
import threading
class gui(threading.Thread): __def __init__(self): ____self.a=0 ____....... ____threading.Thread.__init__(self) __def run(self): ____while 1: ______print self.a ______self.a=self.a+1 gui().start() gtk.main() but in this case print function stopping after gtk.main() shows up im sorry i know im asking a lot :/ :/ im a total noob :/ thanks :) for help EDIT: it starts to print again after i close gtkwindow |
Re: Multitasking and GTK with python
Quote:
try adding writing a to a file, you can see it keeps after you see the gui: Code:
class gui(threading.Thread): |
Re: Multitasking and GTK with python
no i also see terminal window
i mean terminal window and gtk windows gets opened same time and i can see terminal windows while gtkwindow is still running in the background |
Re: Multitasking and GTK with python
class gui(threading.Thread):
__def __init__(self): ____self.a=0 ____....... ____self.entrybox=self.wTree.get_widget('entry2') ____threading.Thread.__init__(self) __def run(self): ____while 1: ______print self.a ______self.entrybox.set_text(a) ______self.a=self.a+1 gui().start() gtk.main() it is not working this way either EDIT: from bluetooth import * class gui(threading.Thread): __def __init__(self): ____....... ____self.entrybox=self.wTree.get_widget('entry2') ____self.sock=BluetoothSocket(RFCOMM) ____self.sock.connect((host,port)) ____print 'connected' ____threading.Thread.__init__(self) __def run(self): ____while 1: ______data=self.sock.recv(1024) ______self.entrybox.set_text(data) gui().start() gtk.main() what i want is to set this device to send and get data whenever it wants. i got a button in gui that starts send function and it work OK but problem is whole about this reading function :/ |
Re: Multitasking and GTK with python
Quote:
|
Re: Multitasking and GTK with python
Don't forget to initialize threads with gobject or else (I suspect) it holds on to the GIL while mainloop is processing and doesn't allowing other threads to run.
I've been using Code:
gtk.gdk.threads_init()Code:
@contextlib.contextmanagerCode:
with gtk_lock():https://garage.maemo.org/scm/?group_id=657 You can also queue tasks to run in the UI thread from your worker thread using idle_add. I use the following helper Code:
def async(func):https://garage.maemo.org/scm/?group_id=1078 |
Re: Multitasking and GTK with python
Code:
import pygtk |
Re: Multitasking and GTK with python
Also, if you use threads with gtk, you need to call
gtk.gdk.threads_init(). pygtk (binding for python and gtk) holds the GIL (which is kind of a global lock for the process) unless threads_init() is called. Try adding this line in the __init__. |
Re: Multitasking and GTK with python
i posted the full code please could you tell me what should i do about it?
edit: yeaa great it worked it worked THANK YOU ALL :d:d |
Re: Multitasking and GTK with python
Quote:
epage mentioned that as well but also extended this to how to handle the GUI itself from within child-threads (also called 'worker threads'), and suggested an alternative - give the gtk to handle your worker threads while it's available using the add_idle example he attached. An advice - If it's your first work with treads, you might want to know more about threads in general and threads in python before diving into gtk's way for handling threads. |
Re: Multitasking and GTK with python
it is great im very excited it worked after adding gtk.gdk.threads_init()
thank you all for all helps im really appreciated |
Re: Multitasking and GTK with python
Just use gobject.io_add_watch to be notified when there is data to read and spare yourself the ordeal of using threads.
http://faq.pygtk.org/index.py?req=sh...=faq20.016.htp def handle_data(source, condition): __data = source.recv(1024) __if len(data) > 0: ____self.entrybox.set_text(self.entrybox.get_text( ) + data) ____# or maybe just self.entrybox.props.text += data ____#print data ____return True # call me again if something happens __else: # end of file ____return False # stop calling me sock=BluetoothSocket(RFCOMM) #sock.settimeout(15) # ? sock.connect(('00:1F:00:B5:3A:45',5)) gobject.io_add_watch(sock, gobject.IO_IN, handle_data) What happens is that your GUI program ALSO watches your socket and should something arrive, it will call "handle_data" which then reads the chunk that arrived (that is fast). After that, your GUI program resumes. Note that threads didn't enter the picture anywhere (not in the GTK library either). |
Re: Multitasking and GTK with python
i made a couple of apps that im using myself
one is for texting over n95 8gb and received text messages via n95 8gb |
| All times are GMT. The time now is 22:18. |
vBulletin® Version 3.8.8