相关疑难解决方法(0)

如何使用python并行查询数据库

我有两个用于查询数据库的函数。假设有两个独立的查询,如何并行运行这些查询以查询同一数据库,并在继续执行其余代码之前,等待两个结果返回?

def query1(param1, param2):
    result = None
    logging.info("Connecting to database...")
    try:
        conn = connect(host=host, port=port, database=db)
        curs = conn.cursor()
        curs.execute(query)
        result = curs
        curs.close()
        conn.close()
    except Exception as e:
        logging.error("Unable to access database %s" % str(e))
    return result


def query2(param1, param2):
    result = None 
    logging.info("Connecting to database...")
    try:
        conn = connect(host=host, port=port, database=db)
        curs = conn.cursor()
        curs.execute(query)
        result = curs
        curs.close()
        conn.close()  
    except Exception as e:
        logging.error("Unable to access database %s" % str(e))    
    return result
Run Code Online (Sandbox Code Playgroud)

python database parallel-processing

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

同时运行两个带参数和返回列表的函数?

据我所知,两个功能可以并行使用运行multiprocessingthreading模块,例如使在同一时间运行两个功能Python的多重并行进程.

但以上示例仅使用打印功能.是否有可能在python中运行并行返回列表的函数,如果是,如何?

我尝试过使用线程:

from threading import Thread
def func1(x):
    return [i*i for i in x]

def func2(x):
    return [i*i*i for i in x]

nums = [1,2,3,4,5]

p1 = Thread(target = func1(nums)).start()
p2 = Thread(target = func2(nums)).start()

print p1
print p2
Run Code Online (Sandbox Code Playgroud)

但我得到了以下错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 761, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: 'list' object is not callable …
Run Code Online (Sandbox Code Playgroud)

python multithreading list

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

在 python 中使用 thread.start() 返回值(使用队列)

我想创建一个函数的多线程版本。我发现t.start()返回None,所以我必须使用队列。我搜索了文档,但我不明白如何在我的示例中使用它。

这是函数:

def derivative(lst, var):  # Example of lst =  [1 + [3 * x]]
    if len(lst) == 1:       
        return derive_solver(lst[0], var)

    if lst[1] == '+':
        return [derivative(lst[0], var), '+', derivative(lst[2], var)]

    if lst[1]  == '*':
        return [[derivative(lst[0], var), '*', lst[2]], '+', [lst[0], '*', derivative(lst[2], var)]]
Run Code Online (Sandbox Code Playgroud)

这是我尝试多线程该功能:

def derivative(lst, var):  # Example of lst =  [1 + [3 * x]]
    if len(lst) == 1:       
        return derive_solver(lst[0], var)

    if lst[1] == '+':
        t1 = threading.Thread(target = derivative, …
Run Code Online (Sandbox Code Playgroud)

python queue multithreading

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

编程理论Gods Strike:Python 2.x中的target = foo是什么?

通常我一直在浏览这个问答网站,使用多线程和处理的答案告诉我使用这样的格式:

(target=foo, args=(bar, baz))
Run Code Online (Sandbox Code Playgroud)

它最常用于多处理多线程(至少我的知识有限.)

我的问题是,什么target意思,有人可以解释它是如何使用的?

我无法在文档或其他地方找到好的解释.

python multithreading multiprocessing

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

地图不归还任何东西

我有以下代码:

def upload_to_s3(filepath, unique_id):
    # do something
    print s3_url # <-- Confirming that this `s3_url` variable is not None
    return s3_url


threads = []
for num, list_of_paths in enumerate(chunked_paths_as_list):
    for filepath in list_of_paths:
        t = threading.Thread(target=upload_to_s3, args=(filepath, self.unique_id))
        t.start()
        threads.append(t)
results = map(lambda t: t.join(), threads)
print results
Run Code Online (Sandbox Code Playgroud)

不幸的是,这是None为每个项目返回:

[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
>>>>> TIME: 13.9884989262
Run Code Online (Sandbox Code Playgroud)

我需要做些什么才能获得上述return声明 …

python multithreading

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