Hi All,
After a few readings about questions related to OpenCascade and
multitask abilities (especially Roman's blog about boolean operations:
http://opencascade.blogspot.com/2008_12_01_archive.html), I still don't
know whether OpenCasade is thread-safe. Do someone here have any
information about that point?
Multithreading is a really important because it could lead to:
- separate the modeling operations and display tasks,
- assign a task per core on multicore machines, and then drastically
improve performances.
Because I don't like to remain in the darkness, I wrote a small sample
that uses the python 'threading' module as well as pythonOCC. Here is
the scenario:
- one thread creates a set of 10 points (gp_Pnt),
- a second thread create vertices from these points.
- exchanges between these threads use a Queue named QUEUE_POINTS.
This sample was committed to the svn repository in a new
'samples/Threading' folder, and the output from this script is pasted
below. It seems to work quite well, the question being now, from my
point of view: 'Can we find an example where multithreading is not
possible? and why?'
Best Regards,
Thomas
===============================================================
C:\Developpement\pythonOCC\tests>python test_thread.py
Create point: (0.22835490981016926, 0.84614076667759275,
0.22413130428937689)
Create vertex from point
Create point: (0.51291891826214742, 0.8912618692170523,
0.091125840440645423)
Create point: (0.99187992643306411, 0.97979231099153252,
0.87428973072316518)
Create vertex from point
Create point: (0.86969820359362282, 0.21588885209097464,
0.35227108176794542)
Create point: (0.046566478550426949, 0.84276148903499182,
0.38475554181509086)
Create vertex from point
Create point: (0.49429781816424478, 0.058480738097280049,
0.37602962921070393)
Create point: (0.81582174758277648, 0.61460020051192521,
0.63983705100329902)
Create vertex from point
Create point: (0.80968164056868841, 0.5308758725237549,
0.069749416218713445)
Create point: (0.33896313246744392, 0.099111266839809309,
0.10691297861174542)
Create vertex from point
Create point: (0.27924271434640269, 0.82596821547990651,
0.90258746833694292)
Create vertex from point
Create vertex from point
Create vertex from point
Create vertex from point
Create vertex from point
Building list from Queue
[<OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex; proxy of <Swig Object of
type 'BRepBuilderAPI_MakeVertex *' at
0x00B31560> >, <OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex; proxy of
<Swig Object of type 'BRepBuilderAPI_M
akeVertex *' at 0x00B31500> >,
<OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex; proxy of <Swig Object of
type 'B
RepBuilderAPI_MakeVertex *' at 0x00B31560> >,
<OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex; proxy of <Swig Ob
ject of type 'BRepBuilderAPI_MakeVertex *' at 0x00B31500> >,
<OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex; pr
oxy of <Swig Object of type 'BRepBuilderAPI_MakeVertex *' at 0x00B31560>
>, <OCC.BRepBuilderAPI.BRepBuilderAPI
_MakeVertex; proxy of <Swig Object of type 'BRepBuilderAPI_MakeVertex *'
at 0x00B31500> >, <OCC.BRepBuilderAPI
.BRepBuilderAPI_MakeVertex; proxy of <Swig Object of type
'BRepBuilderAPI_MakeVertex *' at 0x00B31560> >, <OCC
.BRepBuilderAPI.BRepBuilderAPI_MakeVertex; proxy of <Swig Object of type
'BRepBuilderAPI_MakeVertex *' at 0x00
B31500> >, <OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex; proxy of <Swig
Object of type 'BRepBuilderAPI_MakeVe
rtex *' at 0x00B31560> >, <OCC.BRepBuilderAPI.BRepBuilderAPI_MakeVertex;
proxy of <Swig Object of type 'BRepBu
ilderAPI_MakeVertex *' at 0x00B31500> >] 10
========================================================================================
##Copyright 2009 Thomas Paviot (thomas.pav...@free.fr)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU General Public License for more details.
##
##You should have received a copy of the GNU General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
import threading
import Queue
from OCC.gp import *
from OCC.BRepBuilderAPI import *
import time
import random
QUEUE_POINTS = Queue.Queue(1000)
QUEUE_VERTICES = Queue.Queue(1000)
def CreatePoints():
for i in range(10):
P = gp_Pnt(random.random(),random.random(),random.random())
QUEUE_POINTS.put(P)
time.sleep(0.1) #to make the tasks asynchronous
print "Create point: ",P.Coord()
def CreateVerticesFromPoints():
for i in range(10):
time.sleep(0.2)
P = QUEUE_POINTS.get_nowait()
# Build vertex from point
V = BRepBuilderAPI_MakeVertex(P)
QUEUE_VERTICES.put(V)
print "Create vertex from point"
thread1 = threading.Thread(None, CreatePoints, None,())
thread2 = threading.Thread(None, CreateVerticesFromPoints,None,())
thread1.start()
thread2.start()
# Wait for the tasks to be finished
while thread1.isAlive() or thread2.isAlive():
pass
# Display the content of the queue of vertices:
print "Building list from Queue"
vertices = []
while not QUEUE_VERTICES.empty():
vertex = QUEUE_VERTICES.get_nowait()
if not vertex in vertices: #check that the vertices are different
vertices.append(vertex)
print vertices, len(vertices)
_______________________________________________
Pythonocc-users mailing list
Pythonocc-users@gna.org
https://mail.gna.org/listinfo/pythonocc-users