Running python scripts inside (P)NaCl

233 views
Skip to first unread message

prim...@chromium.org

unread,
Dec 12, 2014, 11:30:56 AM12/12/14
to native-cli...@googlegroups.com, Petr Cermak
Hello Native-Client-Discuss,

In our team we have some python tools (imagine a WSGI daemon + html UI/frontend) and we'd try to turn into a packaged chrome app for the sake of easy of redistribution.
In order to keep the focus on the actual problem, let's assume for the moment that the WSGI python daemon does pure number crunching (other than listening on a tcp port).
Our ideal goal is to run the python scripts inside pnacl and do xhr from the app's html/JS.

Question number 1: Is it feasible doing so? Or does it sound a bad idea by design?

I see that naclports have three python-related projects  (python-host/, python-ppapi/, python-static/). However, I am not sure yet if and which of them can suit our needs.
Does any of the aforementioned projects have pepper bindings (e.g. for stuff like socket and FS api)?
Unfortunately I couldn't find any documentation about the aforementioned python projects.

Any pointers would be appreciated.
Primiano



Matthew Turk

unread,
Dec 12, 2014, 11:45:54 AM12/12/14
to native-cli...@googlegroups.com, Petr Cermak
Hi Primiano,

On Fri, Dec 12, 2014 at 10:30 AM, <prim...@chromium.org> wrote:
> Hello Native-Client-Discuss,
>
> In our team we have some python tools (imagine a WSGI daemon + html
> UI/frontend) and we'd try to turn into a packaged chrome app for the sake of
> easy of redistribution.
> In order to keep the focus on the actual problem, let's assume for the
> moment that the WSGI python daemon does pure number crunching (other than
> listening on a tcp port).
> Our ideal goal is to run the python scripts inside pnacl and do xhr from the
> app's html/JS.

I think this is feasible. I believe Tornado has been ported, although
I haven't tested it to ensure it works with socket permissions.

>
> Question number 1: Is it feasible doing so? Or does it sound a bad idea by
> design?
>
> I see that naclports have three python-related projects (python-host/,
> python-ppapi/, python-static/). However, I am not sure yet if and which of
> them can suit our needs.

python-host is just for building packages that require C libraries and
won't be running on the PNaCl side. python-static will build things
like numpy, matplotlib, etc, but unless you are using C libraries, it
won't be necessary either. (PNaCl, at the time python-static was
developed, required static linking of all extension modules, which
this manages.) python-ppapi is a full-on python interface. Depending
on what you need, either python-ppapi where you execute inside the
produced interpreter or python-static where you do the same would
probably be best.

> Does any of the aforementioned projects have pepper bindings (e.g. for stuff
> like socket and FS api)?

Both should, yes. I know that python-static has both of those.

> Unfortunately I couldn't find any documentation about the aforementioned
> python projects.
>
> Any pointers would be appreciated.
> Primiano
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Native-Client-Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to native-client-di...@googlegroups.com.
> To post to this group, send email to native-cli...@googlegroups.com.
> Visit this group at http://groups.google.com/group/native-client-discuss.
> For more options, visit https://groups.google.com/d/optout.

Matthew Turk

unread,
Dec 12, 2014, 12:38:27 PM12/12/14
to Petr Cermak, native-cli...@googlegroups.com, Primiano Tucci
Hi Petr,

I think the best example of how to start up is the ipython chrome
extension, which is also in naclports. It supplies a script, some UI,
and runs Python.

-Matt

On Fri, Dec 12, 2014 at 11:37 AM, Petr Cermak <petrc...@google.com> wrote:
> Hi Matthew,
>
> Thank you very much for your reply. I managed to install both ports you
> suggested (python-ppapi and python-static), but I have trouble understanding
> how to use them. Is there any sort of PNaCl Hello World example for either
> of them? Or do you happen to know how to start the python-ppapi interactive
> console?
>
> Thanks,
> Petr

Elijah Taylor

unread,
Dec 13, 2014, 2:00:35 AM12/13/14
to native-cli...@googlegroups.com, Petr Cermak, Primiano Tucci
I would also suggest you try running your python code in an existing environment for testing first.  With NaCl Development Environment, you can curl your script into the shell from a webserver, and just run python directly.  This is very similar to the environment I describe below.



It's really easy to embed python in NaCl in your packaged app.  Here's what I've done:

1) grab a prebuilt python zip from the continuous naclports builds like: http://gsdview.appspot.com/naclports/builds/pepper_40/trunk-154-geacd680/publish/python/newlib/python.zip

2) unzip python.zip to the root of your packaged app (this is necessary because of the way naclprocess.js works IIUC, it can't go in a subdirectory)

3) put your python script in the root of the packaged app (this will be accessible from within python at /mnt/html/<yourscript>.py)

4) include naclprocess.js in your page

5) invoke python like this:

  var processManager = new NaClProcessManager();
  processManager.setStdoutListener(function(msg) {
    console.log(msg);
  });

  var command = ['python.nmf', '/mnt/html/<yourscript>.py', 'args-for', 'your-script'];

  processManager.spawn('python.nmf', command, [], '/', 'nacl', null, function(rootPid) {
    processManager.waitpid(rootPid, 0, function(pid, status) {
      // status is the exit code from python
    });
  });


I mostly have used it to read, process, and write files in HTML storage (found at '/mnt/html' in python, PERSISTENT FS in javascript).  I don't know good the socket implementation is in nacl-io.

Amresh Kumar

unread,
Apr 21, 2015, 1:35:40 AM4/21/15
to native-cli...@googlegroups.com, prim...@google.com, petrc...@google.com
Hi Elijah,
I am in a similar situation where I need to run a python script from my packaged app.
I tried the steps you've mentioned here.
I could get python running, but no matter where I place my script file, I always get the error that it was unable to find the file.
Any kind of help would be much appreciated.
Thanks

>> > To post to this group, send email to
>> > native-cli...@googlegroups.com.
>> > Visit this group at
>> > http://groups.google.com/group/native-client-discuss.
>> > For more options, visit https://groups.google.com/d/optout.
>
>

--
You received this message because you are subscribed to the Google Groups "Native-Client-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to native-client-discuss+unsub...@googlegroups.com.

Primiano Tucci

unread,
Apr 21, 2015, 2:04:06 AM4/21/15
to Amresh Kumar, Petr Cermak, native-cli...@googlegroups.com

[this time from the right address]

take a look at the link below, we finally managed to get python running in pnacl:

https://code.google.com/p/chromium/codesearch#chromium/src/tools/memory_inspector/chrome_app/


>> > To post to this group, send email to
>> > native-cli...@googlegroups.com.
>> > Visit this group at
>> > http://groups.google.com/group/native-client-discuss.
>> > For more options, visit https://groups.google.com/d/optout.
>
>

--
You received this message because you are subscribed to the Google Groups "Native-Client-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to native-client-di...@googlegroups.com.

Elijah Taylor

unread,
Apr 21, 2015, 1:13:59 PM4/21/15
to native-cli...@googlegroups.com, Petr Cermak, Primiano Tucci

The easiest place (yet maybe the slowest too) to put your script is at the same location as the embedding page. If you're in an extension/app, both can live at the extension root.

Then the path you pass to naclprocess to spawn your script is /mnt/html/yourscript.py

There is an added benefit here that if you use the HTML mount you will see failed requests clearly in the network tab in devtools to debug what's going on. You will get a bunch of failed network requests for system imports that try to load from the HTML mount too but this is expected.


>> > To post to this group, send email to
>> > native-cli...@googlegroups.com.
>> > Visit this group at
>> > http://groups.google.com/group/native-client-discuss.
>> > For more options, visit https://groups.google.com/d/optout.
>
>

--
You received this message because you are subscribed to the Google Groups "Native-Client-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to native-client-di...@googlegroups.com.

To post to this group, send email to native-cli...@googlegroups.com.
Visit this group at http://groups.google.com/group/native-client-discuss.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Native-Client-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to native-client-di...@googlegroups.com.

Sam Clegg

unread,
Apr 21, 2015, 4:05:13 PM4/21/15
to native-cli...@googlegroups.com, Petr Cermak, Primiano Tucci
I think you mean HTTP mount and /mnt/http/, right?

Elijah Taylor

unread,
Apr 22, 2015, 10:01:19 PM4/22/15
to native-cli...@googlegroups.com, Petr Cermak, Primiano Tucci
Thanks for the correction, yes that is what I meant.
Reply all
Reply to author
Forward
0 new messages