我想从rabbitmq获取消息,然后使用gevent生成greenlet。 我的代码是:
import pika
import gevent.monkey
gevent.monkey.patch_all()
import gevent
from gevent.pool import Pool
from gevent import Timeout
from meliae import scanner
import MySQLdb
import urllib2
from MySQLdb.cursors import SSCursor
db=MySQLdb.connect(host='125.221.225.12',user='root',passwd='young001',charset='utf8',db='delicious',use_unicode=True)
cur = db.cursor()
p = Pool(300)
success_count = 0
fail_count = 0
connection = pika.BlockingConnection(pika.ConnectionParameters(host='125.221.225.12'))
channel = connection.channel()
channel.queue_declare(queue='url_queue')
#logfile = file("log.txt",'aw',buffering=0)
def insert_into_avail(url):
cur.execute("insert into avail_urls(url) values (%s)",url)
db.commit()
def insert_into_fail(url):
cur.execute("insert into fail_urls(url) values (%s)",url)
db.commit()
class TooLong(Exception):
pass
def down(url):
global success_count
global fail_count
print 'the free pool is', p.free_count()
try:
with Timeout(30,TooLong):
url_data = urllib2.urlopen(url)
if url_data and url_data.getcode() ==200:
#print 'url is ok'
insert_into_avail(url)
success_count = success_count+1
else:
print 'the code is ',url_data.getcode()
#logfile.write(url)
#print 'the pool is ', len(p)
print 'success count is', success_count
except:
insert_into_fail(url)
fail_count = fail_count+1
#print 'the url is down',url
#print 'the pool is ', len(p)
print 'the fail_count is', fail_count
for method,properties,body in channel.consume('url_queue'):
#print 'body is ',body
channel.basic_ack(method.delivery_tag)
scanner.dump_all_objects("dump.txt")
p.spawn(down,body)
#scanner.dump_all_objects("dump.txt")
#print 'the pool is ', len(p)
#logfile.write
p.join()
它工作非常慢,有些输出如下:
^{pr2}$它只有大约5个绿叶在游泳池,我想得到300个绿叶在游泳池,这样程序会运行得很快。 怎么了?如何调试?因为rabbitmq,或者gevent?还是我的密码? 泰铢
*更新:*当我评论时”scanner.dump_所有\u对象("转储.txt“),它在全池中运行,可能是因为当我将对象写入文件时,需要时间,所以从rabbitmq获取消息的速度非常慢。在
但是在我的代码中,它似乎有内存泄漏问题,程序占用了数百M内存,所以我想打印所有对象来查找内存泄漏问题?如何修复?有更好的方法吗?
目前没有回答
相关问题 更多 >
编程相关推荐