multithreading - print current thread in python 3 -


i have script:

import threading, socket  x in range(800)     send().start()  class send(threading.thread):     def run(self):         while true:             try:                 s = socket.socket(socket.af_inet, socket.sock_stream)                 s.connect(("www.google.it", 80))                 s.send ("test")                 print ("request sent!")             except:                 pass 

and @ place of "request sent!" print like: "request sent! %s" % (the current number of thread sending request)

what's fastest way it?

--solved--

import threading, socket  x in range(800)     send(x+1).start()  class send(threading.thread):     def __init__(self, counter):         threading.thread.__init__(self)         self.counter = counter     def run(self):         while true:             try:                 s = socket.socket(socket.af_inet, socket.sock_stream)                 s.connect(("www.google.it", 80))                 s.send ("test")                 print ("request sent! @", self.counter)             except:                 pass 

you pass counting number (x, in case), variable in send class. keep in mind though x start @ 0, not 1.

for x in range(800)     send(x+1).start()  class send(threading.thread):     def __init__(self, count):         self.count = count      def run(self):         while true:             try:                 s = socket.socket(socket.af_inet, socket.sock_stream)                 s.connect(("www.google.it", 80))                 s.send ("test")                 print ("request sent!"), self.count             except:                 pass 

or, rob commented above in other question, threading.current_thread() looks satisfactory.


Comments