相关疑难解决方法(0)

Python线程字符串参数

我遇到Python线程问题并在参数中发送字符串.

def processLine(line) :
    print "hello";
    return;
Run Code Online (Sandbox Code Playgroud)

.

dRecieved = connFile.readline();
processThread = threading.Thread(target=processLine, args=(dRecieved));
processThread.start();
Run Code Online (Sandbox Code Playgroud)

其中dRecieved是连接读取的一行字符串.它调用一个简单的函数,到目前为止只有一个打印"hello"的工作.

但是我收到以下错误

Traceback (most recent call last):
File "C:\Python25\lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "C:\Python25\lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: processLine() takes exactly 1 arguments (232 given)
Run Code Online (Sandbox Code Playgroud)

232是我试图传递的字符串的长度,所以我猜它会将其分解为每个字符并尝试传递这样的参数.如果我只是正常调用函数,它工作正常,但我真的想将它设置为一个单独的线程.

python multithreading

131
推荐指数
2
解决办法
16万
查看次数

Python:多线程无法运行

我正在尝试在python中运行一些多线程代码

    from Queue import Queue 
    q = Queue()
    doclist=["adsas","asdasd","oipoipo"]
    for i,doc in enumerate(doclist):
        q.put(doc)
    q.join()

    threadRun.run(50, qWorker.worker(q))
Run Code Online (Sandbox Code Playgroud)

首先,我创建一个队列并向其中添加一些内容。然后,我调用一个创建并运行线程的方法。这是threadRun.run方法

import threading
def run(numThreads,targetMethod):
    print "Running threads"
    for i in range(numThreads):
        t = threading.Thread(target=targetMethod)
        t.daemon=True
        t.start()
Run Code Online (Sandbox Code Playgroud)

这是qWorker.worker方法

def worker(qItem):
    print "Q Worker"
    while True:
            doc = qItem.get()

            try:
                print doc
                qItem.task_done()
            except:
                print "Error"
Run Code Online (Sandbox Code Playgroud)

当我执行以上代码时,什么也没发生。我的方法正确吗?我想念什么?

python multithreading python-2.7

1
推荐指数
1
解决办法
94
查看次数

标签 统计

multithreading ×2

python ×2

python-2.7 ×1