Mohamed Lrhazi wrote:
> Would it make sense to use Cython merely to avoid writing C? That is,
> am I missing something in thinking that I could do that?
>
> Assume I wrote a foo.pyx, which uses an external C lib. foo calls
> functions on the C lib and defines callbacks the latter lib will be
> calling. I then compile the foo.pyx into foo.so, then write a shell
> script that runs: python -c 'import foo'
That should work. It's basically the same as starting a Python module
manually. You have to keep the interpreter alive while the C lib does its
work, though. That usually means calling into some kind of event loop of
the C library that does not return but only calls predefined callbacks (GUI
or networking toolkits come to mind).
Note that you can use pyximport to build the module for you, i.e.
$ ls
foo.pyx
$ export PYTHONPATH=$PWD
$ python -c 'import pyximport; pyximport.install(); import foo'
At least during development, this proves very helpful, as you safe the
extra "run Cython and build the .so" steps.
BTW, it does not seem to work to just run
$ python -m foo
for a "foo.so", the error being "No code object available for foo". Not
sure if there is anything we can do about that...
> Could someone explain, in OS level terms, what would be happening when
> this shell script is running? what would the Python VM be doing? would
> it be idle all the time, while the Cython generated code is all that's
> running?
The Python VM (with which I mean the normal eval loop) would only be
involved when you import or call normal Python code.
> I am looking at using Cython because I need to use Python libs as
> well, but started wondering, if I did not need any Python libs at
> all.. Could I just use Cython for its pythonic syntax and semantics?
Well, Cython is not designed for writing stand-alone C programs. You always
have the overhead of requiring CPython as a dependency. That's great if you
can make use of Python libraries or at least Python data types or
exceptions, but if you use none of those, you just end up with a large,
unused dependency, so that's not the optimal use case.
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev