在开发部署在Apache mod_wsgi上的Django应用程序时,我发现在多线程(Python线程; mod_wsgi进程= 1个线程= 8)的情况下,Python将不会使用所有可用的处理器.使用多处理方法(mod_wsgi processes = 8个线程= 1)一切都很好,我可以完全加载我的机器.
所以问题是:这种Python行为是正常的吗?我怀疑它是因为使用少数线程的1进程是默认的mod_wsgi方法.
该系统是:
FreeBSD 7.2 AMD64和Python 2.6.4上的2xIntel Xeon 5XXX系列(8核(16个超线程))
谢谢大家的回答.我们都发现这种行为是正常的,因为GIL.这是一个很好的解释:http: //jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/ 或stackoverflow GIL讨论:什么是全局解释器锁(GIL)?.
我注意到当我运行我依赖CPU的高级python程序时,它只使用一个核心.运行时是否可以为程序分配多个内核?
我试图使用线程使用python和scipy来解决多个线性系统.对于python线程,我是一个绝对的初学者.我附上了代码,它提炼了我想要完成的事情.此代码有效但执行时间实际上随着totalThreads的增加而增加.我的猜测是,spsolve被视为一个关键部分,实际上并不是同时运行的.
我的问题如下:
我一直在网上搜索答案,但没有运气.也许,我只是使用错误的关键字.谢谢大家的帮助.
def Worker(threadnum, totalThreads):
for i in range(threadnum,N,totalThreads):
x[:,i] = sparse.linalg.spsolve( A, b[:,i] )
threads = []
for threadnum in range(totalThreads):
t = threading.Thread(target=Worker, args=(threadnum, totalThreads))
threads.append(t)
t.start()
for threadnum in range(totalThreads): threads[threadnum].join()
Run Code Online (Sandbox Code Playgroud)
我正在使用Biopython在Python中实现一个算法.我有几个存储在FASTA文件中的对齐(等长序列集).每个对齐包含500到30000个seq,每个序列长约17000个元素.每个序列都存储为Bio.SeqRecord.SeqRecord对象(查看SeqRecord对象的API文档以获取更多信息),该对象不仅包含序列,还包含有关它的一些信息.我使用Bio.AlignIO.read()从磁盘读取它(查看AlignIO模块的API文档以获取更多信息),它返回一个MultipleSeqAlignment对象:
seqs = AlignIO.read(seqs_filename, 'fasta')
len_seqs = seqs.get_alignment_length()
stats = {'-': [0.0] * len_seqs, 'A': [0.0] * len_seqs,
'G': [0.0] * len_seqs, 'C': [0.0] * len_seqs,
'T': [0.0] * len_seqs}
Run Code Online (Sandbox Code Playgroud)
为清晰起见,我将此草图包括在内:

因为我想对对齐的分析进行并行化,所以我使用线程模块为每个可用的cpu分配了一个片段(有关我之后做出此决定的原因的详细信息):
num_cpus = cpu_count()
num_columns = ceil(len_seqs / float(num_cpus))
start_column = 0
threads = []
for cpu in range(0, num_cpus):
section = (start_column, start_column + num_columns)
threads.append(CI_Thread(seqs_type, seqs, section, …Run Code Online (Sandbox Code Playgroud) 工人的目的是什么?这些工作人员是多线程还是其他什么?当Odoo实例启动时,我在命令行上看到至少6名工人通知:
2016-03-10 13:55:09,602 15504 INFO ? openerp.service.server: Worker WorkerHTTP (15504) alive
2016-03-10 13:55:09,606 15503 INFO ? openerp.service.server: Worker WorkerHTTP (15503) alive
2016-03-10 13:55:09,625 15507 INFO ? openerp.service.server: Worker WorkerCron (15507) alive
2016-03-10 13:55:09,628 15506 INFO ? openerp.service.server: Worker WorkerCron (15506) alive
2016-03-10 13:55:09,629 15508 INFO ? openerp.service.server: Worker WorkerCron (15508) alive
2016-03-10 13:55:09,629 15509 INFO ? openerp.service.server: Worker WorkerCron (15509) alive
Run Code Online (Sandbox Code Playgroud)
和之间有什么区别WorkerHTTP和WorkerCron?老实说,我不知道他们做了什么.
我有一个小代码片段如下:
import requests
import multiprocessing
header = {
'X-Location': 'UNKNOWN',
'X-AppVersion': '2.20.0',
'X-UniqueId': '2397123',
'X-User-Locale': 'en',
'X-Platform': 'Android',
'X-AppId': 'com.my_app',
'Accept-Language': 'en-ID',
'X-PushTokenType': 'GCM',
'X-DeviceToken': 'some_device_token'
}
BASE_URI = 'https://my_server.com/v2/customers/login'
def internet_resource_getter(post_data):
stuff_got = []
response = requests.post(BASE_URI, headers=header, json=post_data)
stuff_got.append(response.json())
return stuff_got
tokens = [{"my_token":'EAAOZAe8Q2rKYBAu0XETMiCZC0EYAddz4Muk6Luh300PGwGAMh26Bpw3AA6srcxbPWSTATpTLmvhzkUHuercNlZC1vDfL9Kmw3pyoQfpyP2t7NzPAOMCbmCAH6ftXe4bDc4dXgjizqnudfM0D346rrEQot5H0esW3RHGf8ZBRVfTtX8yR0NppfU5LfzNPqlAem9M5ZC8lbFlzKpZAZBOxsaz'},{"my_token":'EAAOZAe8Q2rKYBAKQetLqFwoTM2maZBOMUZA2w5mLmYQi1GpKFGZAxZCaRjv09IfAxxK1amZBE3ab25KzL4Bo9xvubiTkRriGhuivinYBkZAwQpnMZC99CR2FOqbNMmZBvLjZBW7xv6BwSTu3sledpLSGQvPIZBKmTv3930dBH8lazZCs3q0Q5i9CZC8mf8kYeamV9DED1nsg5PQZDZD'}]
pool = multiprocessing.Pool(processes=3)
pool_outputs = pool.map(internet_resource_getter, tokens)
pool.close()
pool.join()
Run Code Online (Sandbox Code Playgroud)
我所要做的就是将并行POST请求发送到终点,而每个POST都有一个不同的令牌,因为它的帖子正文.
parallel-processing multiprocessing python-2.7 python-requests grequests
我是多线程处理的新手,所以如果我屠宰条款或遗漏一些明显的东西,请原谅我.
下面的代码不会提供任何时间优势,而不是相继调用相同的两个函数的不同代码.
import time
import threading
start_time = time.clock()
def fibonacci(nth): #can be ignored
first = 0
second = 1
for i in range(nth):
third = first + second
first = second
second = third
print "Fibonacci number", i + 1, "is", len(str(first)), "digits long"
def collatz(collatz_max): #can be ignored
for n in range(collatz_max):
n = n + 1 #avoid entering 0
solution = []
solution.append(n)
while n != 1:
if n % 2 == 0:
n = n / …Run Code Online (Sandbox Code Playgroud) 我对 Python 中的线程很陌生,并且有几个初学者问题。
当使用 Pythonthreading模块启动超过五十个线程时,我开始得到MemoryError. 线程本身非常纤细,而且不太需要内存,因此似乎是线程的开销导致了内存问题。
threading模块可以在 Python 代码中运行的最大线程数是多少?你有没有采取任何技巧来达到这个数字?threading模块时还有其他注意事项吗?我一直在尝试编写一个简单的python应用程序来实现一个工作队列,我发现每个网页都有一些随机的人对它进行评论,你不应该使用python线程因为这个或那个,有人可以帮助我吗?什么是Python线程,我可以使用它吗?如果是哪个lib?标准的还不错吗?
使用pyre2(https://github.com/axiak/pyre2)时,我遇到了性能问题(匹配时间)。
我有三个程序:
使用内置re模块的纯Python:https : //gist.github.com/1873402
使用Pyre2的Python:https://gist.github.com/1873402 。(代码的大部分与1号程序相同。除了使用内置re之外,它会将utf-8字符串解码为unicode,使用pyre2时则不需要)
使用re2的C / C ++:https://gist.github.com/1873417
我测量了两个时间:正则表达式预编译时间和匹配时间。
1号程序:1.65s 1.25s
2号程序:0.04s 1.8s
3号程序:0.02s 0.8s
它们都使用相同的正则表达式和输入。(所有正则表达式均受支持re2)
然后,我遵循了有关Cython中性能分析的文档。得到以下结果:
ncalls tottime percall cumtime percall filename:lineno(function)
652884 16.477 0.000 25.349 0.000 re2.pyx:394(_search)
9479 6.059 0.001 41.806 0.004 export_plain.py:60(匹配)
652884 4.243 0.000 33.602 0.000 {搜索're2.Pattern'对象的方法'
652884 4.010 0.000 29.359 0.000 re2.pyx:442(搜索)
652884 3.056 0.000 3.056 0.000 re2.pyx:114(__ init__)
652953 2.145 0.000 2.145 0.000 {实例}
652884 2.002 0.000 2.002 0.000 …