Python - 线程; SyntaxError:关键字参数后非关键字参数

2024-04-25 21:01:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将某个函数作为线程运行,但是

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

Tags: importclienttargetnewdefargsocketthread
3条回答

docs say调用的签名是:

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

你的称呼是:

thread.start_new_thread(target=socketFunctionRead1,())

如您所见,您在非命名参数之前传递的是命名(关键字)参数:您在()之前说的是target=socket...

编辑:澄清一下。解决方法是将关键字删除到第一个参数,或将其添加到第二个参数。

啊!

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中的任何方法都是正确的。

尝试删除目标关键字:

thread.start_new_thread(socketFunctionRead1, ())

这里是documented syntax

thread.start_new_thread(function, args[, kwargs])¶ Start a new thread and return its identifier. The thread executes the function function with the argument list args (which must be a tuple). The optional kwargs argument specifies a dictionary of keyword arguments. When the function returns, the thread silently exits. When the function terminates with an unhandled exception, a stack trace is printed and then the thread exits (but other threads continue to run).

错误的原因在tutorial section on keyword arguments中介绍。简而言之,关键字参数不能在位置参数之前,因为位置变得不明确。

相关问题 更多 >