相关疑难解决方法(0)

为什么不super(Thread,self).__ init __()适用于threading.Thread子类?

我在Python中知道的每个对象都可以通过调用来处理它的基类初始化:

super(BaseClass, self).__init__()
Run Code Online (Sandbox Code Playgroud)

这似乎与子类的情况不同threading.Thread,因为如果我尝试这个SubClass.__init__(),我得到:

RuntimeError: thread.__init__() not called
Run Code Online (Sandbox Code Playgroud)

什么给出了这个错误?我查看了源代码,threading.Thread看起来__init__应该设置该方法Thread.__initialized = True.我看到所有示例都使用以下内容__init__:

class YourThread(threading.Thread):
    def __init__(self, *args):
        threading.Thread.__init__(self)
        # whatev else
Run Code Online (Sandbox Code Playgroud)

但为什么?

python multithreading

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

PyQt:app.exec_()停止运行以下所有代码

我有一个代码,看起来像:

app = QApplication(sys.argv)
self.interface = Interface()

# The figure
self.fig = self.interface.fig
self.ax = self.fig.add_subplot(111)

self.interface.show()
app.exec_()

print 'this is not printed'
Run Code Online (Sandbox Code Playgroud)

问题是,一旦app.exec_()执行,在关闭弹出的窗口之前什么都没有.

如何继续运行代码?

python pyqt pyqt4

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

如何在Python中使用多处理并行求和循环

我很难理解如何使用Python的多处理模块.

我有一个和1n哪里n=10^10,这是太大,无法进入名单,这似乎是在网上使用多很多例子推力.

有没有办法将范围"拆分"为一定大小的段,然后为每个段执行求和?

例如

def sum_nums(low,high):
    result = 0
    for i in range(low,high+1):
        result += i
    return result
Run Code Online (Sandbox Code Playgroud)

而且我想sum_nums(1,10**10)通过将其分解成许多来计算sum_nums(1,1000) + sum_nums(1001,2000) + sum_nums(2001,3000)...,等等.我知道有一个封闭形式,n(n+1)/2但假装我们不知道.

这是我尝试过的

import multiprocessing

def sum_nums(low,high):
    result = 0
    for i in range(low,high+1):
        result += i
    return result

if __name__ == "__main__":
    n = 1000 
    procs = 2 

    sizeSegment = n/procs

    jobs = []
    for i in range(0, procs):
        process = multiprocessing.Process(target=sum_nums, args=(i*sizeSegment+1, (i+1)*sizeSegment))
        jobs.append(process)

    for …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing

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

并行执行过滤器和列表操作的最简单方法

我的程序使用了很多filterall并且列出了理解,我相信它们是并行化的最佳选择(通过多线程或GPU编程)。

那么并行化这些功能的最简单方法是什么?我正在寻找一种相当容易实现的方法(即,不必是最有效的方法或不需要达到最高水平的并行性),以便在潜入更复杂的方法(如PyCUDA等)之前获得潜在的加速效果。

python parallel-processing concurrency gpu

7
推荐指数
0
解决办法
774
查看次数

如果GIL存在,Python中的多线程有什么意义?

据我了解,GIL使得不可能拥有分别利用内核的线程。

这是一个基本问题,但是threading库的意义是什么?如果线程代码的速度与普通程序相当,这似乎没有用。

python multithreading python-multithreading python-multiprocessing

7
推荐指数
3
解决办法
1237
查看次数

为什么这个带有线程的 Python 代码有竞争条件?

此代码创建了一个竞争条件:

import threading

ITERS = 100000
x = [0]

def worker():
    for _ in range(ITERS):
        x[0] += 1  # this line creates a race condition
        # because it takes a value, increments and then writes
        # some inrcements can be done together, and lost

def main():
    x[0] = 0  # you may use `global x` instead of this list trick too
    t1 = threading.Thread(target=worker)
    t2 = threading.Thread(target=worker)
    t1.start()
    t2.start()
    t1.join()
    t2.join()

for i in range(5):
    main()
    print(f'iteration {i}. expected x …
Run Code Online (Sandbox Code Playgroud)

python gil python-3.x

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

来自数据帧熊猫的数据的多线程

我正在努力使用多线程来计算在篮子上有不同购物项目的客户列表之间的相关性.所以我有一个由1,000个客户组成的熊猫数据框,这意味着我必须计算100万次相关性,这需要很长时间来处理

数据框的示例如下所示:

  ID     Item       
    1    Banana    
    1    Apple     
    2    Orange    
    2    Banana    
    2    Tomato    
    3    Apple     
    3    Tomato    
    3    Orange    
Run Code Online (Sandbox Code Playgroud)

这是代码的简化版本:

import pandas as pd

def relatedness (customer1, customer2):
    # do some calculations to measure the relation between the customers

data= pd.read_csv(data_file)
customers_list= list (set(data['ID']))

relatedness_matrix = pd.DataFrame(index=[customers_list], columns=[customers_list])
for i in customers_list:
    for j in customer_list:
        relatedness_matrix.loc[i,j] = relatedness (i,j)
Run Code Online (Sandbox Code Playgroud)

提前致谢!

python multithreading bigdata dataframe

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

如何在保持订单的同时在发电机上使用螺纹?

我有一个简单的代码,对我正在尝试加速的生成器中的每个项目运行GET请求:

def stream(self, records):
    # type(records) = <type 'generator'>
    for record in records:
        # record = OrderedDict([('_time', '1518287568'), ('data', '5552267792')])
        output = rest_api_lookup(record[self.input_field])

        record.update(output)
        yield record
Run Code Online (Sandbox Code Playgroud)

现在,它在单个线程上运行并且永远需要,因为每个REST调用都会等到上一个REST调用完成.

在使用这个很棒的答案(/sf/answers/1992428651/)之前,我已经在列表中使用Python中的多线程,但是我不确定如何在生成器上重用相同的策略而不是名单.

我得到了一位开发人员的建议,他建议我将生成器分解为100个元素列表,然后关闭池,但我不知道如何从生成器创建这些列表.

我还需要保留原始订单,因为我需要yield record按正确的顺序.

python multithreading generator python-multithreading python-2.7

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

如何调试多线程python脚本

我有一个运行几个线程的python脚本。通常,当我想调试python脚本时,请使用“ -m pdb”运行它,然后使用“ b”设置一个断点。但是,由于某种原因,即使它通过了那条线,它也不会在断点处停止,甚至我看到实际上已经添加了断点。知道我在做什么错吗?我从这里使用了一个简单的python线程模块示例

import threading

class SummingThread(threading.Thread):
     def __init__(self,low,high):
         threading.Thread.__init__(self)
         self.low=low
         self.high=high
         self.total=0

     def run(self):
         print 'self.low = ' + str(self.low) + ', self.high = ' + str(self.high)
         for i in range(self.low,self.high):
             self.total+=i

thread1 = SummingThread(0,500000)
thread2 = SummingThread(500000,1000000)
thread1.start() # This actually causes the thread to run
thread2.start()
thread1.join()  # This waits until the thread has completed
thread2.join()
# At this point, both threads have completed
result = thread1.total + thread2.total
print result
Run Code Online (Sandbox Code Playgroud)

然后run,使用 …

python debugging

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

有没有更快的方法来解析这个文本文件?

我正在从一些类似于以下内容的文本文件中解析日期/时间/测量信息:

[Sun Jul 15 09:05:56.724 2018] *000129.32347
[Sun Jul 15 09:05:57.722 2018] *000129.32352
[Sun Jul 15 09:05:58.721 2018] *000129.32342
[Sun Jul 15 09:05:59.719 2018] *000129.32338
[Sun Jul 15 09:06:00.733 2018] *000129.32338
[Sun Jul 15 09:06:01.732 2018] *000129.32352
Run Code Online (Sandbox Code Playgroud)

结果进入输出文件,如下所示:

07-15-2018 09:05:56.724, 29.32347
07-15-2018 09:05:57.722, 29.32352
07-15-2018 09:05:58.721, 29.32342
07-15-2018 09:05:59.719, 29.32338
07-15-2018 09:06:00.733, 29.32338
07-15-2018 09:06:01.732, 29.32352
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码如下所示:

import os
import datetime

with open('dq_barorun_20180715_calibtest.log', 'r') as fh, open('output.txt' , 'w') as fh2:
    for line in fh:
        line = line.split()
        monthalpha = …
Run Code Online (Sandbox Code Playgroud)

python optimization file

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