user3687294
user3687294

Reputation: 31

Python unable to start multiple threads

I have the following python code - creating three threads. But the control does not seem to come back after the first thread is created. The output is one '.', instead of three expected '.'s. Is there any problem with the worker function? Msglyr is an internal messaging library.

#!/usr/bin/python

import thread
import time
import Msglyr

# Worker thread
def worker(num):
    testchannel = Msglyr.Channel("TEST-ASYNC-ROUTER-DEALER-CHANNEL", Msglyr.mlr.ASYNC_ROUTER_DEALER, Msglyr.mlr.ASYNC_DEALER_WORKER)
    testchannel.setEndPoint(Msglyr.mlr.ASYNC_DEALER_WORKER, "inproc://test-pt")
    testchannel.setEndPoint(Msglyr.mlr.ASYNC_DEALER_CLIENT, "tcp://127.0.0.1:5558")

    while True :
        msg = Msglyr.Message()
        testchannel.receive(msg)
        if msg.getnumParts() > 0:
            msg.setPart(msg.getnumParts() - 1, "Worker : " + str(num))
        testchannel.send(msg)

# Creating three threads
try:
   thread.start_new_thread( worker, (1, ) )
   print '.'
   time.sleep(2)

   thread.start_new_thread( worker, (2, ) )
   print '.'
   time.sleep(2)

   thread.start_new_thread( worker, (3, ) )
   print '.'
   time.sleep(2)
except:
   print "Error: unable to start thread"

print 'Started threads'
while 1:
   pass

Upvotes: 2

Views: 1029

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148870

I'm not sure this is really an answer (not what you expected anyway) but I cannot do more. I took your code and replaced worker with

# Worker thread
def worker(num):
    i = 1
    while True :
        print("Thread", num, i);
        i += 1
        time.sleep(3)

All goes fine, I can see messages form 3 threads (1 second interval ...) and I also see the 3 dots. I'm afraid the problem lies in Msglyr.

Upvotes: 1

Related Questions