Halite Home

Using PyPy for Python bots


#1

I'd like to use PyPy to run my python bot, since I don't use any third-party package, if pypy was on the server it should just run with no problems. However, since pypy isn't on the server, I was going to just run a portable version. I was following @nmalaguti guide: https://2016.forums.halite.io/t/how-to-submit-a-bot-in-any-language/423

So, I made an install.sh file:

#!/bin/bash

# fail on any error
set -e

# cd to the directory of the script
cd "$(dirname "$0")"

# use curl to download any runtime dependencies
curl -sL https://bitbucket.org/squeaky/portable-pypy/downloads/pypy-5.6-linux_x86_64-portable.tar.bz2
tar -xf ./pypy-5.6-linux_x86_64-portable.tar.bz2

Then MyBot.py looks like this:

from subprocess import call
from glob import glob
import os
import sys
import logging

# cd to the script directory
os.chdir(os.path.dirname(os.path.realpath(__file__)))

# look for the node executable
pypybin = glob('pypy*/bin/pypy')
if len(pypybin) == 0:
    # if it isn't found we're probably running locally
    pypybin = 'pypy'
else:
    pypybin = pypybin[0]

# call the script with the relative path to node and hook up stdin, stdout, and stderr
call(pypybin + " ./pypyBot.py", shell=True)

Unfortunately, I'm getting an error that says that pypy isn't found. Anybody have some ideas on this?


#2

@b7500af1 Edit: found the problem. curl -sL will send the result to stdout, not to a file on disk. If you want to save the archive to disk, you need to use -o to specify a filename.

curl -sL -o pypy-5.6-linux_x86_64-portable.tar.bz2 https://bitbucket.org/squeaky/portable-pypy/downloads/pypy-5.6-linux_x86_64-portable.tar.bz2
tar -xf ./pypy-5.6-linux_x86_64-portable.tar.bz2

# or

curl -sL https://bitbucket.org/squeaky/portable-pypy/downloads/pypy-5.6-linux_x86_64-portable.tar.bz2 | tar -x

#3

@nmalaguti Thanks for the reply. Unfortunately, that's not working.

Do you know if I can get any sort of error report from the install.sh script? Seems like that should come in the "Compilation successful" email that I get.


#4

What if you try using | tar -xj to specify bz2 explicitly?

I may have made a mistake and had | tar -xf just do | tar -x, no f.


#5

No luck so far.


#6

@truell20 Do you have any advice on how to tell if my install.sh script is working?

Looking at the compile script, it seems that Python builds get an auto-pass on compilation success. Does that hide the result of the install.sh script?


#7

OK, I got it working. Here's what you need for anyone with a Python bot that wants to use PyPy. If you have pure python, and only use pure python 3rd party libraries (not Numpy), then you can get some pretty nice performance gains from PyPy (40x or so I've seen).

You need this in your install.sh file:

#!/bin/bash

# use curl to download portable pypy
curl -svLO https://bitbucket.org/squeaky/portable-pypy/downloads/pypy-5.6-linux_x86_64-portable.tar.bz2

tar -xf ./pypy-5.6-linux_x86_64-portable.tar.bz2

And then in your MyBot.py file, I call my actual bot script with:

from subprocess import call
from glob import glob
import os

# cd to the script directory
os.chdir(os.path.dirname(os.path.realpath(__file__)))

# look for the pypy executable
pypybin = glob('pypy*/bin/pypy')
if len(pypybin) == 0:
    # if it isn't found we're probably running locally
    pypybin = 'pypy'
else:
    pypybin = pypybin[0]

# call the script with the relative path to pypy
call(pypybin + " ./pypyBot.py", shell=True)

How to submit a bot in any language
#8

You can now mark your bot as running on PyPy if you would like. Just add a LANGUAGE file to the root directory of your zip submission and put the "PyPy" in there.