由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Python做计算怎么只用一个核?
相关主题
C++里面如何最方便的表示这个数组的数组?python+ beautifulsoup 爬网页怎么那么复杂?
什么工具可以查出这种错?那位大侠介绍一下python的webcrawler吧
问一个python multiprocessing问题python regexp question
python的muti process和multi threading有啥优劣?问个用python scratch yelp html 数据的问题
python下怎么解决GIL?有啥好轮子可以抓取网页里的不规则信息?
请教html中的hrefMatLab Code
同主题转寄 (转载)a few very simple python commands
请问哪里有python的code exampleC -> assembly
相关话题的讨论汇总
话题: __话题: import话题: python
进入Programming版参与讨论
1 (共1页)
p********e
发帖数: 6030
1
做些简单的科学计算,把程序拿到学校的workstation上发现跑起来和我的笔记本差不
多快,一看八个核里只有一个在跑,怎么能让Python多用几个核一起跑呢?
p***o
发帖数: 1252
2
要这么简单就能用八个核大家全得失业。

【在 p********e 的大作中提到】
: 做些简单的科学计算,把程序拿到学校的workstation上发现跑起来和我的笔记本差不
: 多快,一看八个核里只有一个在跑,怎么能让Python多用几个核一起跑呢?

p********e
发帖数: 6030
3

。。。这居然还是门技术?那八个核难道就是让我一边run仿真一边灌水用的?

【在 p***o 的大作中提到】
: 要这么简单就能用八个核大家全得失业。
k**********g
发帖数: 989
4

有一种绝活叫做茅台穿针 (Maotai Threading)
http://www.tutorialspoint.com/python/python_multithreading.htm

【在 p********e 的大作中提到】
: 做些简单的科学计算,把程序拿到学校的workstation上发现跑起来和我的笔记本差不
: 多快,一看八个核里只有一个在跑,怎么能让Python多用几个核一起跑呢?

t****t
发帖数: 6806
5
最简单的办法是同时跑8个仿真.

【在 p********e 的大作中提到】
:
: 。。。这居然还是门技术?那八个核难道就是让我一边run仿真一边灌水用的?

p**o
发帖数: 3409
6
python3的concurrent.futures有一些便利,可以在一个with语句块里多进程。
python2可以自己摸索一下multiprocessing
偷懒的话可以参考我下面的代码(从我自己写函数库里扒出来的),
实现的是一个比较通用的进程池链(把几个进程池串联起来)。
后面附一个使用示例。
要睡觉没时间慢慢解释了,看得懂就用,看不懂就算了...
文件 parallel.py
# -*- coding: utf-8 -*-
#
#
""" Helper functions and templates for multiprocessing.
http://docs.python.org/2/library/multiprocessing.html#examples
http://hg.python.org/cpython/file/3.3/Lib/concurrent/futures/pr
http://www.ibm.com/developerworks/aix/library/au-threadingpytho
"""
__all__ = [
'ProcessPoolsChain',
]
import os, sys, time
from multiprocessing import Manager, Process, Pool, Pipe, Queue
def worker (func, inque, outque):
""" Worker process.
"""
for index, item in iter(inque.get, '__STOP__'):
# the 2nd form of iter:
# the callable is called until it returns the sentinel
output = func(item)
outque.put((index, output))
class ProcessPoolsChain (object):
""" Asynchronous worker-process pools chained by queues.
"""
def __init__ (self, *funcs_workers):
""" Constructor for ProcessPoolsChain.
e.g. ProcessPoolsChain( (func1, num1), (func2, num2) )
means num1 workers for func1, num2 for func2.
"""
self.numpools = len(funcs_workers)
self.numworkerslist = []
self.queues = [Queue() for _ in xrange(self.numpools+1)]
for i, (func, numworkers) in enumerate(funcs_workers):
self.numworkerslist.append(numworkers)
for _ in xrange(numworkers):
Process(target=worker, args=(
func, self.queues[i], self.queues[i+1]
)).start()
def feed (self, inputs):
""" Feeds a sequence of data into the (first) input queue, and
obtains
a sequence of outputs that match the input order.
"""
numinputs = len(inputs)
outputs = [None for _ in xrange(numinputs)]
for index, item in enumerate(inputs):
self.queues[0].put((index, item))
# get results
finished = set()
while len(finished) < len(outputs):
index, result = self.queues[-1].get() # blocked
outputs[index] = result
finished.add(index)
return outputs
def stop (self):
""" Stops the whole pools chain.
"""
for i in xrange(self.numpools):
numworkers = self.numworkerslist[i]
for j in xrange(numworkers):
self.queues[i].put('__STOP__')
示例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import sys
import time
import urllib2
from BeautifulSoup import BeautifulSoup
# The worker functions must be placed on the module level, because
# the multiprocessing API have to import this module
def urlchunk (host):
url = urllib2.urlopen(host)
chunk = url.read()
return chunk
def datamine (chunk):
soup = BeautifulSoup(chunk)
return soup.findAll(['title'])
# print '__name__==', __name__ # '__parents_main__' if imported by
multiprocessing
if __name__ == '__main__':
# IMPORTANT NOTICE: the construction of ProcessPoolsChain must not carry
# '__main__' as __name__, because the multiprocessing API will import
# this module, with __name__=='__parents_main__'.
# Putting this code on the module level would spawn infinite
# number of processes!!
import parallel
ppc = parallel.ProcessPoolsChain(
(urlchunk, 4),
(datamine, 2)
)
t0 = time.time()
urls = [
"http://google.com",
"http://yahoo.com",
"http://google.com",
"http://amazon.com",
"http://ibm.com",
"http://apple.com",
"http://www.mit.edu",
"http://www.cs.umn.edu",
]
results = ppc.feed(urls)
ppc.stop()
for i, res in enumerate(results):
print i, res
print('Done. Time elapsed: %.2f.' % (time.time() - t0))
# test 1 single pool
ppc1 = parallel.ProcessPoolsChain(
(urlchunk, 4)
)
results1 = ppc1.feed(urls)
ppc1.stop()
for i, chunk in enumerate(results1):
print i, len(chunk)

【在 p********e 的大作中提到】
: 做些简单的科学计算,把程序拿到学校的workstation上发现跑起来和我的笔记本差不
: 多快,一看八个核里只有一个在跑,怎么能让Python多用几个核一起跑呢?

w***g
发帖数: 5958
7
用scipy和并行的blas就可以了. python跟C/C++不一样, 本身的运行效率已经比裸机低
了好几十倍. 用python写的程序都是不需要效率的, 所以也没有人去把python的并行化
做做好.

【在 p********e 的大作中提到】
: 做些简单的科学计算,把程序拿到学校的workstation上发现跑起来和我的笔记本差不
: 多快,一看八个核里只有一个在跑,怎么能让Python多用几个核一起跑呢?

c*******y
发帖数: 1630
8
python的MT还是单核应该,GIL在那里。
要多核得multiprocess

【在 k**********g 的大作中提到】
:
: 有一种绝活叫做茅台穿针 (Maotai Threading)
: http://www.tutorialspoint.com/python/python_multithreading.htm

c*******y
发帖数: 1630
9
谁说python不讲效率? pypy试试看,挺快的了。

【在 w***g 的大作中提到】
: 用scipy和并行的blas就可以了. python跟C/C++不一样, 本身的运行效率已经比裸机低
: 了好几十倍. 用python写的程序都是不需要效率的, 所以也没有人去把python的并行化
: 做做好.

o****n
发帖数: 348
10
用python notebook可以多核

【在 p********e 的大作中提到】
: 做些简单的科学计算,把程序拿到学校的workstation上发现跑起来和我的笔记本差不
: 多快,一看八个核里只有一个在跑,怎么能让Python多用几个核一起跑呢?

c*********n
发帖数: 182
11
有个东西叫GIL知道么?
s*********e
发帖数: 1051
12
pypy不支持numpy :-(

【在 c*******y 的大作中提到】
: 谁说python不讲效率? pypy试试看,挺快的了。
p********e
发帖数: 6030
13
大家说得都太复杂了,GIL什么的确实没有听过,还是这个方法最简单,不过也不大实
用因为我也没有那么多要同时跑。

【在 t****t 的大作中提到】
: 最简单的办法是同时跑8个仿真.
1 (共1页)
进入Programming版参与讨论
相关主题
C -> assemblypython下怎么解决GIL?
学习multi threading, 有什么好书?请教html中的href
[合集] First impression on Python同主题转寄 (转载)
这问题有没有好办法做?请问哪里有python的code example
C++里面如何最方便的表示这个数组的数组?python+ beautifulsoup 爬网页怎么那么复杂?
什么工具可以查出这种错?那位大侠介绍一下python的webcrawler吧
问一个python multiprocessing问题python regexp question
python的muti process和multi threading有啥优劣?问个用python scratch yelp html 数据的问题
相关话题的讨论汇总
话题: __话题: import话题: python