欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

win下 python 多线程运行失败的问题

程序员文章站 2022-05-02 13:05:18
...

最近拿python写个语音通讯软件,要使用多线程,但运行后出现了奇怪的现象,仅有一个线程工作 而后面的线程不运行 像被阻塞了,感觉很奇怪....

这里要注意 各位朋友要养成 Try: except的好习惯...



本来以为是threading 在windows下不兼容的问题,后来使用thread 仍然失败,然后编写了最简单的例程发现 thread函数中 必须要写一个参数 才能正常调用成功。。

也就是如下代码

def test_a()
    while(True):
        sleep(5)
        print  'xx'
def test_b():
    while(True):
       sleep(5)
       print  'xx'
theading.thread(test_b())   ##这么调用是失败的,我们增加一个参数给test_a(num), test_b(num)
theading.thread(test_a, args=(6,))   #这么调用才可以,希望各位朋友注意。。。



import socket
import sys
import threading
# import thread
from time import sleep


def tcp_server(db):
    HOST = ''  # Symbolic name meaning all available interfaces
    PORT = 5000  # Arbitrary non-privileged port

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print 'Socket created'

    try:
        s.bind((HOST, PORT))
    except socket.error, msg:
        print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
        sys.exit()

    print 'Socket bind complete'

    s.listen(10)
    print 'Socket now listening'

    # now keep talking with the client
    while 1:
        # wait to accept a connection - blocking call
        conn, addr = s.accept()
        print 'Connected with ' + addr[0] + ':' + str(addr[1])
        while (True):
            data = conn.recv(1024)
            reply = 'OK...' + data
            if not data:
                break

            conn.sendall(reply)

    conn.close()
    s.close()


def tcp_client(db):
    try:
        # create an AF_INET, STREAM socket (TCP)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, msg:
        print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
        sys.exit();

    print 'Socket Created client'
    # host = 'www.google.com'
    host=''
    port = 5000

    # try:
    #     host = socket.gethostname()
    #     remote_ip=socket.gethostbyaddr(host)
    #
    # except socket.gaierror:
    #     # could not resolve
    #     print 'Hostname could not be resolved. Exiting'
    #     sys.exit()

    # print 'Ip address of ' + host + ' is ' + remote_ip
    # Connect to remote server

    try:
        s.connect(('127.0.0.1', port))
    except Exception,e:
        print 'connect error'
        print e
        sys.exit()
        # print 'Socket Connected to ' + host + ' on ip ' + remote_ip
        # Send some data to remote server
    message = "GET / HTTP/1.1\r\n\r\n"


    while (True):
        try:
            # Set the whole string
            s.sendall(message)
        except Exception, e:
            # Send failed
            print e
            sys.exit()

        print 'Message send successfully'

        # Now receive data
        reply = s.recv(4096)

        print 'from server:', reply
        sleep(5)

if __name__ == "__main__":
    t=threading.Thread(target=tcp_server,args=(2,))
    t.start()
    sleep(3)
    s=t=threading.Thread(target=tcp_client,args=(3,))

    s.start()
    t.join()
    s.join()


import socket
import sys
import threading
# import thread
from time import sleep


def tcp_server(db):
    HOST = ''  # Symbolic name meaning all available interfaces
    PORT = 5000  # Arbitrary non-privileged port

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print 'Socket created'

    try:
        s.bind((HOST, PORT))
    except socket.error, msg:
        print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
        sys.exit()

    print 'Socket bind complete'

    s.listen(10)
    print 'Socket now listening'

    # now keep talking with the client
    while 1:
        # wait to accept a connection - blocking call
        conn, addr = s.accept()
        print 'Connected with ' + addr[0] + ':' + str(addr[1])
        while (True):
            data = conn.recv(1024)
            reply = 'OK...' + data
            if not data:
                break

            conn.sendall(reply)

    conn.close()
    s.close()


def tcp_client(db):
    try:
        # create an AF_INET, STREAM socket (TCP)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, msg:
        print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
        sys.exit();

    print 'Socket Created client'
    # host = 'www.google.com'
    host=''
    port = 5000

    # try:
    #     host = socket.gethostname()
    #     remote_ip=socket.gethostbyaddr(host)
    #
    # except socket.gaierror:
    #     # could not resolve
    #     print 'Hostname could not be resolved. Exiting'
    #     sys.exit()

    # print 'Ip address of ' + host + ' is ' + remote_ip
    # Connect to remote server

    try:
        s.connect(('127.0.0.1', port))
    except Exception,e:
        print 'connect error'
        print e
        sys.exit()
        # print 'Socket Connected to ' + host + ' on ip ' + remote_ip
        # Send some data to remote server
    message = "GET / HTTP/1.1\r\n\r\n"


    while (True):
        try:
            # Set the whole string
            s.sendall(message)
        except Exception, e:
            # Send failed
            print e
            sys.exit()

        print 'Message send successfully'

        # Now receive data
        reply = s.recv(4096)

        print 'from server:', reply
        sleep(5)

if __name__ == "__main__":
    t=threading.Thread(target=tcp_server,args=(2,))
    t.start()
    sleep(3)
    s=t=threading.Thread(target=tcp_client,args=(3,))

    s.start()
    t.join()
    s.join()