Python : threads; SyntaxError: non-keyword arg after keyword arg
-
08-03-2021 - |
Question
I wanted to run a certain function as a thread, but I get
SyntaxError: non-keyword arg after keyword arg
And I do not understand why :
#!/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()
I want to open these sockets as threads. I do not get why I get the non-keyword error, as the function I want to run as a thread is socketFunctionRead1
error :
File "pythonbis.py", line 79
thread.start_new_thread(target=socketFunctionRead1,())
SyntaxError: non-keyword arg after keyword arg
Solution
The docs say the the signature for the thread.start_new_thread
call is:
thread.start_new_thread(function, args[, kwargs])
The way you are calling it is:
thread.start_new_thread(target=socketFunctionRead1,())
As you can see you are passing a named (keyword) argument before the non-named one: you are saying target=socket...
before ()
.
EDIT: just to clarify. The solution is either to remove the keyword to the first argument or to add it to the second.
HTH!
OTHER TIPS
thread.start_new_thread(target=socketFunctionRead1,())
thread.start_new_thread(target=socketFunctionWrite1,())
thread.start_new_thread(target=socketFunctionRead2,())
thread.start_new_thread(target=socketFunctionWrite2,())
Needs to be
thread.start_new_thread((),target=socketFunctionRead1)
thread.start_new_thread((),target=socketFunctionWrite1)
thread.start_new_thread((),target=socketFunctionRead2)
thread.start_new_thread((),target=socketFunctionWrite2)
You can't have positional arguments after keyword arguments, which is true for any method in python.
Try removing the target keyword:
thread.start_new_thread(socketFunctionRead1, ())
Here is the 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).
The reason for the error is covered in the tutorial section on keyword arguments. In short, keyword arguments cannot precede positional arguments because the position becomes ambiguous.