Python:线程; SyntaxError:关键字arg之后的非关键字arg

时间:2011-11-24 19:38:36

标签: python multithreading

我想将某个函数作为一个线程运行,但我得到了

SyntaxError: non-keyword arg after keyword arg

我不明白为什么:

#!/usr/bin/env python
import sys
import arduinoReadThread
import arduinoWriteThread
import socket
import thread

bolt = 0
socketArray=list()
HOST ="localhost"
PORT1 =50000
PORT2 =50001

def readAndParseArgv():
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
    s.bind((HOST,PORT1)) #bind to that port
    print "test"
    s.listen(2) #listen for user input and accept 1 connection at a time.
    socketArray.append(s)
    s2=socket.socket(socket.AF_INET, socket.SOCK_STREAM ) #create an INET, STREAMing socket
    s2.bind((HOST,PORT2)) #bind to that port
    print "test"
    s2.listen(2) #listen for user input and accept 1 connection at a time.
    socketArray.append(s2)

def socketFunctionWrite1():
    print threadName
    client, address = s1.accept()
    while(bolt == 0):
        print "Writing connections"
        if len(s1ToWriteList) > 0:
            client.send(s1ToWriteList.pop(0))

def socketFunctionRead1():
    client, address = s2.accept()
    while(bolt == 0):
        f = client.recv(1024)
        print "reading connection"
        s1ToWriteList.append(f)
        print len(s1ToWriteList)

def socketFunctionWrite2():
    client, address = s2.accept()
    while(bolt == 0):
        print "Writing connections"
        if len(s2ToWriteList) > 0:
            client.send(s2ToWriteList.pop(0))

def socketFunctionRead2():
    client, address = s1.accept()
    while(bolt == 0):
        f = client.recv(1024)
        print "reading connection"
        s2ToWriteList.append(f)
        print len(s2ToWriteList)

def shutDown():
    test = raw_input("Quit ?")
    if(test =="y"):
        bolt = 1
    else:
        shutDown()

thread.start_new_thread(target=socketFunctionRead1,())
thread.start_new_thread(target=socketFunctionWrite1,())
thread.start_new_thread(target=socketFunctionRead2,())
thread.start_new_thread(target=socketFunctionWrite2,())

readAndParseArgv()
spreadSockets()

我想将这些套接字打开为线程。我不明白为什么我得到非关键字错误,因为我想作为一个线程运行的函数是socketFunctionRead1

错误:

  File "pythonbis.py", line 79
    thread.start_new_thread(target=socketFunctionRead1,())
SyntaxError: non-keyword arg after keyword arg

3 个答案:

答案 0 :(得分:1)

thread.start_new_thread来电的签名为docs say

thread.start_new_thread(function, args[, kwargs])

你打电话的方式是:

thread.start_new_thread(target=socketFunctionRead1,())

正如您所看到的那样,您正在传递一个名为(strong)的参数 之前的非命名参数:您在target=socket...之前说()

编辑:只是为了澄清。解决方案是将关键字删除到第一个参数或将其添加到第二个参数。

HTH!

答案 1 :(得分:1)

thread.start_new_thread(target=socketFunctionRead1,())
thread.start_new_thread(target=socketFunctionWrite1,())
thread.start_new_thread(target=socketFunctionRead2,())
thread.start_new_thread(target=socketFunctionWrite2,())

需要

thread.start_new_thread((),target=socketFunctionRead1)
thread.start_new_thread((),target=socketFunctionWrite1)
thread.start_new_thread((),target=socketFunctionRead2)
thread.start_new_thread((),target=socketFunctionWrite2)

在关键字参数之后,你不能有位置参数,对于python中的任何方法都是如此。

答案 2 :(得分:0)

尝试删除目标关键字:

thread.start_new_thread(socketFunctionRead1, ())

以下是documented syntax

  

thread.start_new_thread(function,args [,kwargs])¶启动一个新线程   并返回其标识符。线程执行函数功能   使用参数列表args(必须是元组)。可选的   kwargs参数指定关键字参数的字典。当。。。的时候   函数返回,线程默默退出。当功能   以未处理的异常终止,打印堆栈跟踪   然后线程退出(但其他线程继续运行)。

tutorial section on keyword arguments涵盖了错误原因。简而言之,关键字参数不能位于位置参数之前,因为位置变得模糊不清。