python 解释器, 扩展模块
2011-03-31 07:44
232 查看
// MyTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "python.h"
#include <process.h>
#include <windows.h>
#include <io.h>
#include <Fcntl.h>
#include <python-ast.h>
#include <string>
using namespace std;
unsigned int __stdcall PythonRoute( void * pData )
{
::PyEval_AcquireLock();
PyThreadState * pState = ::Py_NewInterpreter();
PyEval_SaveThread();
for( int i = 0; i < 100000; i++ )
{
// PyRun_SimpleString( "pass" );
printf( "++%d/n", i );
Sleep( 500 );
}
//int nRet = PyRun_SimpleString( "import sys" );
//nRet = PyRun_SimpleString( "sys.path.append( /"Z:////Python313Src////Python-3.1.3////tspy/" )" );
//PyObject * pModule = ::PyImport_ImportModule( "AA" );
//PyObject * pErr = ::PyErr_Occurred();
//if( pErr != NULL )
//{
// PyObject *exception, *v, *tb;
// ::PyErr_Fetch( &exception, &v, &tb );
// PyErr_NormalizeException(&exception, &v, &tb);
// if (tb == NULL) {
// tb = Py_None;
// Py_INCREF(tb);
// }
// PyException_SetTraceback(v, tb);
// PyObject *m1 = PyImport_AddModule("io");
// PyObject *dict = PyModule_GetDict(m1);
// PyObject *pType = PyDict_GetItemString( dict, "StringIO" );
// PyObject*a = PyTuple_New(0);
// PyObject*pObj = ::PyObject_Call( pType, a, NULL );
//
// PyObject * p1 = ::PySys_GetObject( "stderr" );
// PyObject * p2 = ::PySys_GetObject( "__stderr__" );
// ::PySys_SetObject( "stderr", pObj );
// ::PySys_SetObject( "__stderr__", pObj );
// ::PyErr_Display( exception, v, tb );
// ::PySys_SetObject( "stderr", p1 );
// ::PySys_SetObject( "__stderr__", p2 );
// PyObject* pD = PyObject_Dir(pObj) ;
// PyObject * pFunc = PyObject_GenericGetAttr(pObj, PyUnicode_FromString("getvalue") );
// PyObject * pUStr = PyObject_CallObject( pFunc, a );
// PyObject * pAStr = ::PyUnicode_AsASCIIString( pUStr );
// char * str = ::PyBytes_AsString( pAStr );
// Py_DECREF(pObj);
// PyErr_Restore(exception, v, tb);
//}
PyEval_RestoreThread(pState);
Py_EndInterpreter( pState );
::PyEval_ReleaseLock();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
::Py_Initialize();
PyEval_InitThreads();
PyThreadState * p =PyEval_SaveThread();
// PyCodeObject *co = NULL;
// struct _mod *mod;
// PyCompilerFlags flags;
// PyArena *arena = PyArena_New();
// if (arena == NULL)
// return NULL;
//FILE *fp;
//fopen_s( &fp, "Z://Python313Src//Python-3.1.3//tspy//AA.py" , "r" );
// flags.cf_flags = 0;
// mod = PyParser_ASTFromFile(fp, "Z://Python313Src//Python-3.1.3//tspy//AA.py", NULL,
// Py_file_input, 0, 0, &flags,
// NULL, arena);
//asdl_seq * pseq = mod->v.Module.body;
//for( int i = 0; i < asdl_seq_LEN( pseq ); i++ )
//{
// _stmt * pstmt = (_stmt * )asdl_seq_GET( pseq, i );
// if( pstmt->kind == FunctionDef_kind )
// {
// PyObject * pp = pstmt->v.FunctionDef.name;
// PyObject * pp1 = ::PyUnicode_AsASCIIString( pp );
// char * s = ::PyBytes_AsString( pp1 );
// printf(s );
// asdl_seq * pseq1 = pstmt->v.FunctionDef.body;
// _stmt * pstmt1 = (_stmt * )asdl_seq_GET( pseq1, 0 );
// if( pstmt1->kind == Expr_kind )
// {
// int k =1;
// }
// }
//}
HANDLE hThread=NULL;;
unsigned threadID;
printf( "Creating second thread.../n" );
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &PythonRoute, NULL, 0, &threadID );
for( int i = 0; i < 100000; i++ )
{
PyEval_RestoreThread( p );
PyRun_SimpleString( "pass" );
PyEval_SaveThread();
printf( "--%d/n", i );
Sleep( 500 );
}
// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
// Destroy the thread object.
CloseHandle( hThread );
Py_Finalize();
return 0;
}
//
#include "stdafx.h"
#include "python.h"
#include <process.h>
#include <windows.h>
#include <io.h>
#include <Fcntl.h>
#include <python-ast.h>
#include <string>
using namespace std;
unsigned int __stdcall PythonRoute( void * pData )
{
::PyEval_AcquireLock();
PyThreadState * pState = ::Py_NewInterpreter();
PyEval_SaveThread();
for( int i = 0; i < 100000; i++ )
{
// PyRun_SimpleString( "pass" );
printf( "++%d/n", i );
Sleep( 500 );
}
//int nRet = PyRun_SimpleString( "import sys" );
//nRet = PyRun_SimpleString( "sys.path.append( /"Z:////Python313Src////Python-3.1.3////tspy/" )" );
//PyObject * pModule = ::PyImport_ImportModule( "AA" );
//PyObject * pErr = ::PyErr_Occurred();
//if( pErr != NULL )
//{
// PyObject *exception, *v, *tb;
// ::PyErr_Fetch( &exception, &v, &tb );
// PyErr_NormalizeException(&exception, &v, &tb);
// if (tb == NULL) {
// tb = Py_None;
// Py_INCREF(tb);
// }
// PyException_SetTraceback(v, tb);
// PyObject *m1 = PyImport_AddModule("io");
// PyObject *dict = PyModule_GetDict(m1);
// PyObject *pType = PyDict_GetItemString( dict, "StringIO" );
// PyObject*a = PyTuple_New(0);
// PyObject*pObj = ::PyObject_Call( pType, a, NULL );
//
// PyObject * p1 = ::PySys_GetObject( "stderr" );
// PyObject * p2 = ::PySys_GetObject( "__stderr__" );
// ::PySys_SetObject( "stderr", pObj );
// ::PySys_SetObject( "__stderr__", pObj );
// ::PyErr_Display( exception, v, tb );
// ::PySys_SetObject( "stderr", p1 );
// ::PySys_SetObject( "__stderr__", p2 );
// PyObject* pD = PyObject_Dir(pObj) ;
// PyObject * pFunc = PyObject_GenericGetAttr(pObj, PyUnicode_FromString("getvalue") );
// PyObject * pUStr = PyObject_CallObject( pFunc, a );
// PyObject * pAStr = ::PyUnicode_AsASCIIString( pUStr );
// char * str = ::PyBytes_AsString( pAStr );
// Py_DECREF(pObj);
// PyErr_Restore(exception, v, tb);
//}
PyEval_RestoreThread(pState);
Py_EndInterpreter( pState );
::PyEval_ReleaseLock();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
::Py_Initialize();
PyEval_InitThreads();
PyThreadState * p =PyEval_SaveThread();
// PyCodeObject *co = NULL;
// struct _mod *mod;
// PyCompilerFlags flags;
// PyArena *arena = PyArena_New();
// if (arena == NULL)
// return NULL;
//FILE *fp;
//fopen_s( &fp, "Z://Python313Src//Python-3.1.3//tspy//AA.py" , "r" );
// flags.cf_flags = 0;
// mod = PyParser_ASTFromFile(fp, "Z://Python313Src//Python-3.1.3//tspy//AA.py", NULL,
// Py_file_input, 0, 0, &flags,
// NULL, arena);
//asdl_seq * pseq = mod->v.Module.body;
//for( int i = 0; i < asdl_seq_LEN( pseq ); i++ )
//{
// _stmt * pstmt = (_stmt * )asdl_seq_GET( pseq, i );
// if( pstmt->kind == FunctionDef_kind )
// {
// PyObject * pp = pstmt->v.FunctionDef.name;
// PyObject * pp1 = ::PyUnicode_AsASCIIString( pp );
// char * s = ::PyBytes_AsString( pp1 );
// printf(s );
// asdl_seq * pseq1 = pstmt->v.FunctionDef.body;
// _stmt * pstmt1 = (_stmt * )asdl_seq_GET( pseq1, 0 );
// if( pstmt1->kind == Expr_kind )
// {
// int k =1;
// }
// }
//}
HANDLE hThread=NULL;;
unsigned threadID;
printf( "Creating second thread.../n" );
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &PythonRoute, NULL, 0, &threadID );
for( int i = 0; i < 100000; i++ )
{
PyEval_RestoreThread( p );
PyRun_SimpleString( "pass" );
PyEval_SaveThread();
printf( "--%d/n", i );
Sleep( 500 );
}
// Wait until second thread terminates. If you comment out the line
// below, Counter will not be correct because the thread has not
// terminated, and Counter most likely has not been incremented to
// 1000000 yet.
WaitForSingleObject( hThread, INFINITE );
// Destroy the thread object.
CloseHandle( hThread );
Py_Finalize();
return 0;
}
相关文章推荐
- Python 解释器自动载入的模块
- Python学习之使用ctypes模块操作C扩展程序
- python c模块扩展及PyIntObj对象
- Python模块扩展之AES算法
- Python扩展模块的细节(
- Windows下Python添加MySQLdb扩展模块
- 使用Visual Studio.Net 2005开发Python扩展模块
- 快速实现python c扩展模块
- 【python笔记】扩展模块
- 快速实现python c扩展模块
- Python内置的操作系统模块(os)与解释器交互模块(sys)
- Windows下Python添加MySQLdb扩展模块
- Python 数据处理扩展包: pandas 模块的DataFrame介绍(创建和基本操作)
- fedora下的ganglia安装、配置,以及python模块的扩展
- 扩展Python__在python脚本中使用其他语言(c/c++/java/c#)编写的模块
- Python扩展模块——selenium的使用(定位、下载文件等)
- Windows下安装Python扩展模块提示“Unable to find vcvarsall.bat”的问题
- Python可视化的扩展模块matplotlib的简单应用
- 用C语言编写Python扩展模块
- <转>Ganglia的Python扩展模块开发