python 多线程问题

这里面每个线程都是一个死循环 只能等服务器那边停止发送消息,才会停止,多线程能同时执行吗?否则下面的线程执行不到,必须等第一个线程执行完下面才能执行

 for (room_name,room_id) in json_data.items():
        if room_id!='0':
            print(room_name+":"+room_id)
            if room_name=="SecooShanghai":
                p1=threading.Thread(target=douyin_webcast.excute_main, args=(room_name,room_id,))
                p1.start()
                p1.join()
            if room_name=="dyxbhjks5p73":
                p2 = threading.Thread(target=douyin_webcast.excute_main, args=(room_name,room_id,))
                p2.start()
                p2.join()
            if room_name=="Secoobeijing":
                p3=threading.Thread(target=douyin_webcast.excute_main, args=(room_name,room_id,))
                p3.start()
                p3.join()
            if room_name=="DYSECOO":
                p4 =threading.Thread(target=douyin_webcast.excute_main, args=(room_name,room_id,))
                p4.start()
                p4.join()

最佳答案

join方法移到循环外 全部线程start之后再join

 prolist = []
 for (room_name,room_id) in json_data.items():
        if room_id!='0':
            print(room_name+":"+room_id)
            if room_name=="SecooShanghai":
                p1=threading.Thread(...)
                p1.start()
                prolist.append(p1)
            if room_name=="dyxbhjks5p73":
                p2 = threading.Thread(...)
                p2.start()
                prolist.append(p2)
            if room_name=="Secoobeijing":
                p3=threading.Thread(...)
                p3.start()
                prolist.append(p3)
            if room_name=="DYSECOO":
                p4 = threading.Thread(...)
                p4.start()
                prolist.append(p4)
  for p in prolist:
    p.join()