Hi,

Here is the code attached. It is a simple test program that open a gtk
window and connect to a server (might be an irc or telnet server).
When the button "Connect" is clicked, the thread (should) starts and
read what is coming from the server. But it never happens... just when
I click on the "Disconnect" button and the join() function is called.
Did you get any problem like that?

BR,

David

On 2/25/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
> David Cohen wrote:
> > Hi all,
> >
> > I have a problem trying to use thread on python.
> > I import the threading module and execute this:
> >
> > def func_thread():
> >     something...
> >
> > new_thread = threading.Thread(target = func_thread)
> > new_thread.start()
> >
> > But the thread never really starts, just when I call the
> > new_thread.join() method.
> > Does anybody could help me?
>
> More code would help. What you have shown looks fine. What does
> func_thread() do? How do you know it isn't doing it? Here is a very
> simple, working example based on your code:
>
> import threading, time
>
> def func_thread():
>      while True:
>          print 'threading'
>          time.sleep(1)
>
> new_thread = threading.Thread(target = func_thread)
> new_thread.start()
>
> print "main thread going away now"
>
> Kent
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
#!/usr/bin/python2.4

import pygtk
pygtk.require('2.0')
import gtk
import socket
import threading

class GetSockData(threading.Thread):
	def __init__(self, sock, tview):
		threading.Thread.__init__(self)
		self.sock = sock
		self.tview = tview
		self.i_am_alive = True
	def run(self):
		buf = self.tview.get_buffer()
		while self.i_am_alive:
			msg = self.sock.recv(1024)
			buf.insert_at_cursor(msg)
	def stop(self):
		self.i_am_alive = False
		

class MainWindow:
	def delete_event(self, widget, event, data=None):
		print "delete event occurred"
		return False

	def destroy(self, widget, data=None):
		if self.is_connected:
			self.sock.close()
			self.getsock.stop()
			self.getsock.join()
			self.is_connected = False
		gtk.main_quit()
	
	def text_sent_event(self, entry):
		text = entry.get_text()
		buf = self.tview_main.get_buffer()
		if self.is_connected:
			self.sock.send(text + "\r\n")
		else:
			buf.insert_at_cursor("Your are not connected!\n")
		buf.insert_at_cursor(text + "\n")
		entry.set_text("")
	
	def clicked_disconnect_event(self, button):
		self.sock.close()
		self.is_connected = False
		self.tview_main.get_buffer().insert_at_cursor("Disconnected\n")
		self.getsock.stop()
		self.getsock.join()
	
	def clicked_connect_event(self, button):
		if self.is_connected:
			print "Client already connected!\n"
			return
		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.sock.connect(("localhost", 6667))
		self.is_connected = True
		self.tview_main.get_buffer().insert_at_cursor("Connected!\n")
		self.getsock = GetSockData(self.sock, self.tview_main)
		self.getsock.start()

	def __init__(self):
		window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		window.connect("delete_event", self.delete_event)
		window.connect("destroy", self.destroy)
		window.set_border_width(5)
		window.set_default_size(640, 480)
		window.set_title("Test Socket")
		window.set_resizable(True)

		self.is_connected = False

		vbox = gtk.VBox(False, 5)
		window.add(vbox)

		hbox = gtk.HBox(True, 5)
		vbox.pack_start(hbox, False, False)

		btn_connect = gtk.Button("Connect")
		hbox.pack_start(btn_connect, True, False)
		btn_connect.connect("clicked", self.clicked_connect_event)
		btn_disconnect = gtk.Button("Disconnect")
		hbox.pack_start(btn_disconnect, True, False)
		btn_disconnect.connect("clicked", self.clicked_disconnect_event)
		vbox.pack_start(gtk.HSeparator(), False, False)

		ws_main = gtk.ScrolledWindow()
		ws_main.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		ws_main.set_placement(gtk.CORNER_BOTTOM_LEFT)
		tbuf = gtk.TextBuffer()
		self.tview_main = gtk.TextView(tbuf)
		self.tview_main.set_editable(False)
		ws_main.add_with_viewport(self.tview_main)
		vbox.pack_start(ws_main, True, True)

		self.entry_cmd = gtk.Entry()
		self.entry_cmd.connect("activate", self.text_sent_event)
		self.entry_cmd.grab_focus()
		vbox.pack_start(self.entry_cmd, False, False)

		list_focus = [ self.entry_cmd ]
		vbox.set_focus_chain(list_focus)
		
		window.show_all()

	def main(self):
		gtk.main()

if __name__ == "__main__":
	window = MainWindow()
	window.main()

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to