Ex10: Successfull pyBind11
This commit is contained in:
		
							parent
							
								
									c5f7489717
								
							
						
					
					
						commit
						c6e48cf47c
					
				
					 4 changed files with 48 additions and 5 deletions
				
			
		
							
								
								
									
										20
									
								
								ex10/main.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								ex10/main.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | |||
| #!/usr/bin/env python3 | ||||
| 
 | ||||
| import ThreadPool | ||||
| 
 | ||||
| if __name__ == "__main__": | ||||
|     print(dir(ThreadPool)) | ||||
|     tp = ThreadPool.ThreadPool() | ||||
| 
 | ||||
|     print("Creating Consumers") | ||||
|     tp.addConsumer( 2000, 20 ) | ||||
|     tp.addConsumer( 2000, 10 ) | ||||
| 
 | ||||
| 
 | ||||
|     print("Create Producers") | ||||
|     tp.addProducer( 4000, 5 ) | ||||
| 
 | ||||
|     print("Run all Threads") | ||||
|     tp.run() | ||||
| 
 | ||||
|     print("End of Program") | ||||
							
								
								
									
										8
									
								
								ex10/makefile
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								ex10/makefile
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,8 @@ | |||
| main: | ||||
| 	g++ -pthread ThreadPool.cc main.cpp | ||||
| 
 | ||||
| threadpool: | ||||
| 	g++ -pthread -D ENABLE_PYBIND=1 -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` ThreadPool.cc -o ThreadPool`python3-config --extension-suffix` | ||||
| 
 | ||||
| clean: | ||||
| 	rm -rf *.so __pycache__ | ||||
|  | @ -19,22 +19,19 @@ class ThreadPool | |||
| 			_consumers(), | ||||
| 			_producers() | ||||
| 		{}; | ||||
| 		auto addProducer( int max_t = 3000, int max_n = 10) | ||||
| 		void addProducer( int max_t = 3000, int max_n = 10) | ||||
| 		{ | ||||
| 			std::thread * tp = new std::thread( producer, _producers.size(), std::ref(_q), max_t, max_n ); | ||||
| 
 | ||||
| 			_producers.push_back(tp); | ||||
| 
 | ||||
| 			return tp; | ||||
| 		} | ||||
| 
 | ||||
| 		auto addConsumer( int max_t = 2000, int max_n = 10) | ||||
| 		void addConsumer( int max_t = 2000, int max_n = 10) | ||||
| 		{ | ||||
| 			std::thread * tp = new std::thread( consumer, _consumers.size(), std::ref(_q), max_t, max_n ); | ||||
| 
 | ||||
| 			_consumers.push_back(tp); | ||||
| 
 | ||||
| 			return tp; | ||||
| 		} | ||||
| 
 | ||||
| 		void run() | ||||
|  | @ -61,4 +58,22 @@ class ThreadPool | |||
| 		// queue
 | ||||
| 		SharedQueue<int> _q; | ||||
| }; | ||||
| 
 | ||||
| #if ENABLE_PYBIND | ||||
| /*
 | ||||
|  * Define Python access | ||||
|  */ | ||||
| #include <pybind11/pybind11.h> | ||||
| 
 | ||||
| namespace py = pybind11; | ||||
| 
 | ||||
| PYBIND11_MODULE(ThreadPool, tp) | ||||
| { | ||||
| 	py::class_<ThreadPool>( tp, "ThreadPool") | ||||
| 		.def(py::init()) | ||||
| 		.def("addProducer", &ThreadPool::addProducer, "Add a new Producer to the Pool") | ||||
| 		.def("addConsumer", &ThreadPool::addConsumer, "Add a new Consumer to the Pool") | ||||
| 		.def("run", &ThreadPool::run, "Run all registered Producers and Consumers"); | ||||
| } | ||||
| #endif | ||||
| #endif | ||||
|  |  | |||
		Reference in a new issue