1 # -*- coding: utf-8 -*-
2 ############################################################
3 # Copyright © 2005-2010 Benoît PIN <benoit.pin@ensmp.fr> #
4 # Plinn - http://plinn.org #
6 # This program is free software; you can redistribute it #
7 # and/or modify it under the terms of the Creative Commons #
8 # "Attribution-Noncommercial 2.0 Generic" #
9 # http://creativecommons.org/licenses/by-nc/2.0/ #
10 ############################################################
11 """ Image threaded batch computation module
17 from types
import StringTypes
20 from ZODB
.POSException
import ConflictError
21 from cStringIO
import StringIO
23 console
= logging
.getLogger('[manipulation thread]')
25 class ImageQueueProcessorThread(threading
.Thread
) :
26 """This thread is started at zope startup
32 def __init__(self
, portal_path
, itemsPath
) :
33 threading
.Thread
.__init
__(self
)
34 self
.portal_path
= portal_path
36 if isinstance(itemsPath
, StringTypes
) :
37 itemsPath
= [itemsPath
]
43 return len(self
.queue
)
45 def queueAdd(self
, itemPath
) :
46 self
.queue
.append(itemPath
)
49 console
.info('process started.')
50 #atexit.register(self.stop)
53 while not self
.__stopped
and self
.queueSize
:
59 console
.info('process finished.')
61 #print con.transaction_manager
65 console
.info('process stopped.')
68 def _process(self
, app
) :
69 path
= self
.queue
.pop(0)
71 p
= app
.unrestrictedTraverse(path
)
73 console
.warn('deleted during processing: %s' % path
)
76 console
.info('%d : %s' % (self
.queueSize
, p
.absolute_url()))
79 if not hasattr(p
, 'thumbnail'):
81 # print 'make thumbnail'
83 for size
in ((500, 500), (600, 600), (800, 800)) :
84 # print 'resize at', size
85 p
._getResizedImage
(size
, True)
88 zMin
= p
.tiles_min_zoom
89 zMax
= p
.tiles_max_zoom
90 zStep
= p
.tiles_step_zoom
91 levels
= range(zMin
, zMax
+ zStep
, zStep
)
92 zooms
= [l
/100. for l
in levels
]
93 todo
= set(zooms
) - set(p
._tiles
.keys())
95 if p
.tileGenerationLock
.locked() :
96 console
.info('skip %s: already tiling.' % p
.absolute_url())
99 p
.tileGenerationLock
.acquire()
107 # print 'tiling at', zoom
109 rppm
= ppm
.resize(ratio
=zoom
)
112 p
._makeTilesAt
(zoom
, rppm
)
117 p
.tileGenerationLock
.release()
120 delattr(p
, '_v__methodResultsCache')
121 except AttributeError:
124 p
.tiles_available
= 1
125 p
.reindexObject(idxs
=['tiles_available'])
128 except ConflictError
:
129 console
.warn('Resync after ZODB ConflicError')
131 portal
= app
.unrestrictedTraverse(self
.portal_path
)
136 p
.tiles_available
= -1
139 traceback
.print_exc(None, out
)
140 console
.error(out
.getvalue())