0

I'm try to grab bullet screens by python. The process is that I will get the response after sending login and some others messages to bullet screens server.

When I plan to grab two rooms's bullet screens, I can only get the response of the first request.

Code is like this:

# coding=utf-8

import multiprocessing
import socket
import time
import re
import signal
import threading



client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname("openbarrage.douyutv.com")
port = 8601
client.connect((host, port))

def send_req_msg(msg):
    """
    wrap msg
    """
    pass

def set_msg(roomid):
    msg = 'type@=loginreq/roomid@={}/\0'.format(roomid)
    send_req_msg(msg)

    msg_more = 'type@=joingroup/rid@={}/gid@=-9999/\0'.format(roomid)
    send_req_msg(msg_more)



if __name__ == '__main__':
    pool = []
    roomid = [123, 666]
    for i in range(0, len(roomid)):
        t = threading.Thread(target=set_msg(roomid[i]))
        t.start()
        pool.append(t)
    for a in pool:
        a.join()

Both multiprocessing and threading can't succeed. As I use wireshard to analysis the tcp. There is only the first request and it's response. Also I can only get the room's screen bullets of 123. So why the second thread/processing doesn't work? And what should I do? Thanks.

5
  • When I ran your code send_req_msg() was called 4 times with different arguments so that's correct. If you think messages aren't sent try to print response from server after sending message if there's any. Commented Nov 23, 2018 at 16:52
  • add some print statements at entry to set_msg and send_req_msg, so you can see that they are called and what the parameter value is they are passed. Also add a print statement at the end of each function so you can see when they exit. Commented Nov 23, 2018 at 17:01
  • If you are on Windows, multiprocessing always starts a whole new process so trying to share a global like client won't work - the whole python file is re-executed so the second instance will get a different client connection. Look at the help 16.6.3.2 in this page docs.python.org/2/library/multiprocessing.html. AFAICT when using Multiprocessing, properly and completely reading and understanding the manual pages is critical for success. Commented Nov 23, 2018 at 17:09
  • @barny I had add many print statements and can only get print statements of first thread. I will read the page ,thanks.(I'm on mac) Commented Nov 24, 2018 at 3:16
  • @kevin has solved your problem. Commented Nov 24, 2018 at 15:05

1 Answer 1

2
t = threading.Thread(target=set_msg(roomid[i]))

This does not pass the function set_msg() to the thread. It calls the function and passes the result to the thread (in this case, the value passed is always None because set_msg() does not return anything). If you call the function before creating the thread, then the function will not run in the thread. You need to pass the function (and its arguments) to the Thread constructor without calling it:

t = threading.Thread(target=set_msg, args=(roomid[i],))
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, this is my fault. And I will try it tonight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.