Hi. I am trying to make a script that will print out numbers in rapid succession after working a little with them. The script needs to write them in the console one after another (next line). The code works just fine when I do not utilize multi threading, but when I do, I still get the right numbers, but from time to time there is a white space at the beginning of the line. That is a major issue, because then the program fails to work (when it is being tested.

Is there a way to get several threads to work together, printing to the console very close to each other without this bug?

here is some of the code:

class myThread(threading.Thread):
	def __init__ ( self, value, coins ):
	      self.val = value
	      self.coi = coins
	      threading.Thread.__init__ ( self )

   	def run ( self ):
    	 	someMethod(self.coi, self.val)#this prints a value after working a little.

#SOME CODE
#SOME CODE
#SOME CODE
#SOME CODE
#SOME CODE


for line in stdin:
   	myThread(int(line),coins).start()

I get output that is a variation of:
3
8
4

with or without a single space in front of some (or all) of the numbers.

Thanks for the help:)

What does your print statement look like?

It a little complicated. But the essence of it can be lookt at as this:

def run ( self ):
     print someMethod(self.coi, self.val)
     
    def someMethod(a, b):
     for i in a:
      if b<i:
       return i-b

The problem I have is that this method is run by several threads at ones, with the same values for what i have called "coi" (an array of ints).

It seems to me that you are printing without checking that someMethod actually returns a value rather than none, unless you are entirely certain that some i in a will be greater than b. Was this the same as in the single-thread problem?

If you have read my initial post it sais "the code works just fine...". The problem is siply that there are _extra_ spaces that appear when I run the multi threade version. It seems as though one of the threads, for no apparent reason, decides to add a space in front of the print statement.

I have overcome this problem with a rather unoptimized solution, where all the threads store the value to a common array, that is later printed out. This does indeed work, but removes parts of the "paralell-ness" of the program, a part I tried to introduce to gain some performance. It is no longer essential to me to get an answer, because this assignment belongs to the past. But non the less it would be nice to know the reason for this strange behavior.

If you have read my initial post it sais "the code works just fine...". The problem is siply that there are _extra_ spaces that appear when I run the multi threade version. It seems as though one of the threads, for no apparent reason, decides to add a space in front of the print statement.

I have overcome this problem with a rather unoptimized solution, where all the threads store the value to a common array, that is later printed out. This does indeed work, but removes parts of the "paralell-ness" of the program, a part I tried to introduce to gain some performance. It is no longer essential to me to get an answer, because this assignment belongs to the past. But non the less it would be nice to know the reason for this strange behavior.

You could try 2 things:

First idea: define a global Threading.Lock object that each thread must acquire to print a value

lock.acquire()
print value
lock.release()

Second idea: let your threads put the values in a Queue.Queue object, and start a single thread to read values in the queue and print them

# in working threads
thequeue.put(value)

# in the single printing thread
while True:
  value = thequeue.get()
  thequeue.task_done()
  print value

I was also facing similar issue. Just add a carriage return at the end of print.
eg: print("your print\r")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.