Yesterday i tried closure , One can easily use the function as variable
You can see my session here and use of closure :
...:) B-)
Reference :
![]() |
| Android Client |
![]() |
| Python Server |
import threading
import datetime
class ThreadClass(threading.Thread):
def run(self):
now = datetime.datetime.now()
print "%s says Hello World at time: %s" %
(self.getName(), now)
for i in range(2):
t = ThreadClass()
t.start()
Another more simplistic example :
import time from threading import Thread def myfunc(i): print "sleeping 5 sec from thread %d" % i time.sleep(5) print "finished sleeping from thread %d" % i for i in range(10): t = Thread(target=myfunc, args=(i,)) t.start()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use
#!/usr/bin/python # This is server.py fileimport socket # Import socket modules = socket.socket() # Create a socket objecthost = socket.gethostname() # Get local machine nameport = 12345 # Reserve a port for your service. print 'Server started!'print 'Waiting for clients...'s.bind((host, port)) # Bind to the ports.listen(5) # Now wait for client connection.c, addr = s.accept() # Establish connection with client.print 'Got connection from', addrwhile True: msg = c.recv(1024) print addr, ' >> ', msg msg = raw_input('SERVER >> ') c.send(msg); #c.close() # Close the connection#!/usr/bin/python # This is client.py fileimport socket # Import socket modules = socket.socket() # Create a socket objecthost = socket.gethostname() # Get local machine nameport = 12345 # Reserve a port for your service.print 'Connecting to ', host, ports.connect((host, port))while True: msg = raw_input('CLIENT >> ') s.send(msg) msg = s.recv(1024) print 'SERVER >> ', msg#s.close # Close the socket when done