0

I am having a big program and i am using threading for it. I have a problem here so my threads looks as shown below.

t1 = threading.Thread(target=broadcast, name="Send_Broadcast", args=(bd_of_the_machine, 5678))
t2 = threading.Thread(target=bd_recv_controller, name="Receive_Broadcast", args=(bd_of_the_machine, 5432))
t1.start()
t2.start()
t6 = threading.Thread(target=createKeySpace)
t6.start()
t3=threading.Thread(target=reply_for_req,name="Send_Rep",args=(ip_of_the_machine,5367))



q = Queue()
t4 = threading.Thread(target=bd_recv_wireless, args=(bd_of_the_machine,5694,q))
t4.start()
Host = (q.get())
q2=Queue()
t5 = threading.Thread(target=subscriber_wireless, name="Receive_publisher_msgs", args=(Host, 6782,q2))
t5.start()
recv_string=(q2.get())
t7 = threading.Thread(target=insert_wireless, name="insert", args=(recv_string,))
t7.start()



q3 = Queue()
t8=threading.Thread(target=bd_recv_wired, args=(bd_of_the_machine,3727,q3))
t8.start()
Host1 = (q3.get())
q4=Queue()
t9=threading.Thread(target=subscriber_wired, name="Receive_publisher_msgs", args=(Host1, 6564,q4))
t9.start()
recv_string1=(q4.get())
t10=threading.Thread(target=insert_wired,name="insert_wired",args=(recv_string1,))
t10.start()

I am not joining any thread but it just runs in one after another. For example t5 thread needs to run asynchronously and t9 too. But since i start t5 at the beginning only after i finished my work with t5 its not going to run t9.

Is there any fix to this problem.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
KSp
  • 1,199
  • 1
  • 11
  • 29
  • 1
    I would advise you against using threads like this in Python, if you're dealing with IO you should use any of the asyncio solutions out there, I would go with trio. – yorodm Jun 15 '19 at 15:54
  • Yes you are right but I just didn't imagine, threads would lock everything. – KSp Jun 15 '19 at 16:21

1 Answers1

3

Your question is missing details about the target functions, but I am going to try and guess that some of them call external C/C++ modules.

In this case take a look at this answer

So, whether an I/O bound module isn't releasing the GIL, or one of the functions is computationally heavy and messes with your OS scheduler. Since you seem to be dealing with network I guess the first option is the right one.

If the modules are of your own creation, then you have multiple options:

  • Understand how to make the module calls thread-safe, and release the GIL.
  • Use multiprocessing and inter-process communication.
  • Use another python implementation that doesn't have a GIL (e.g Jython and IronPython)

If the modules are downloaded modules you found on the internet:

  • Use another, more appropriate C-extension
  • Edit them to match your requirements - and maybe pull request the changes since I/O bound modules should release the GIL

Note that built in I/O bound functions do release the GIL (as you can see here)

Nomios
  • 151
  • 7