我在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)
但为什么?
我有一个代码,看起来像:
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的多处理模块.
我有一个和1到n哪里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) 我的程序使用了很多filter,all并且列出了理解,我相信它们是并行化的最佳选择(通过多线程或GPU编程)。
那么并行化这些功能的最简单方法是什么?我正在寻找一种相当容易实现的方法(即,不必是最有效的方法或不需要达到最高水平的并行性),以便在潜入更复杂的方法(如PyCUDA等)之前获得潜在的加速效果。
据我了解,GIL使得不可能拥有分别利用内核的线程。
这是一个基本问题,但是threading库的意义是什么?如果线程代码的速度与普通程序相当,这似乎没有用。
python multithreading python-multithreading python-multiprocessing
此代码创建了一个竞争条件:
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) 我正在努力使用多线程来计算在篮子上有不同购物项目的客户列表之间的相关性.所以我有一个由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)
提前致谢!
我有一个简单的代码,对我正在尝试加速的生成器中的每个项目运行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
我有一个运行几个线程的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,使用 …
我正在从一些类似于以下内容的文本文件中解析日期/时间/测量信息:
[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 ×10
bigdata ×1
concurrency ×1
dataframe ×1
debugging ×1
file ×1
generator ×1
gil ×1
gpu ×1
optimization ×1
pyqt ×1
pyqt4 ×1
python-2.7 ×1
python-3.x ×1