2

I have this script:

import threading, socket

for 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 at the place of "Request sent!" I would like to print something like: "Request sent! %s" % (the current number of the thread sending the request)

What's the fastest way to do it?

--SOLVED--

import threading, socket

for 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
Allexj
  • 1,375
  • 6
  • 14
  • 29
  • Possible duplicate of [How to find a thread id in Python](http://stackoverflow.com/questions/919897/how-to-find-a-thread-id-in-python) – Rob Sep 02 '16 at 07:31

3 Answers3

4

Just a side answer to how to get the thread ID of the current thread (might not respond directly to the question but help others): In python 3.3+ you can do simply :

import threading

threading.get_ident()

Read more : here

Gazihan Alankus
  • 11,256
  • 7
  • 46
  • 57
Anatol Bivol
  • 890
  • 7
  • 20
1

You could pass your counting number (x, in this case), as a variable in your send class. Keep in mind though that x will start at 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, as Rob commented above in the other question, threading.current_thread() looks satisfactory.

TerryA
  • 58,805
  • 11
  • 114
  • 143
  • thanks for the answer. It raises: [code] raise RuntimeError("thread.__init__() not called") RuntimeError: thread.__init__() not called [/code] – Allexj Sep 02 '16 at 07:45
  • solved by adding "threading.Thread.__init__(self)" to __init__ function. thanks – Allexj Sep 02 '16 at 08:06
  • but i still can't understand why send(x+1).start() has this +1 – Allexj Sep 02 '16 at 08:08
  • 1
    @allexj It's because `range(800)` starts with 0 not 1, so when we say "Request sent!" for the first time we're saying "this is the first one!" instead of "this is the zeroth one!" – TerryA Sep 02 '16 at 09:16
1

The easiest way to do this is to use setName and getName to give names to your threads.

import threading, socket

for x in range(800)
    new_thread = send()
    new_thread.setName("thread number %d" % x)
    new_thread.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 by %s!" % self.getName())
            except:
                pass

You can also add any other attributes to send that you need to keep track of your threads.

Bi Rico
  • 25,283
  • 3
  • 52
  • 75