python - how do I create a bytearray of a certain size?

beavoid used Ask the Experts™
My RTS clients need to send a HELO message to the server, 1024 bytes long, with a MSG type value at index MSG_HELO = 0.

Should the whole show be 1024 bytes long? or will messages with the game-state attached from server to client need maybe 4096?

Maybe a different length for start-up and a different length for in-game?

Start-up clients send small size A to server? how small?
In-game server sends big size to clients? how big?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Do you try`multiprocessing` pipes and `send()` like the topic?
import multiprocessing as mp

def big_array(conn, size=1200):
    a = "a" * size
    print "Child process trying to send array of %d floats." %size
    return a

if __name__ == "__main__":
    print "Main process started."
    parent_conn, child_conn = mp.Pipe()
    proc = mp.Process(target=big_array, args=[child_conn, 120000])
    print "Child process started."
    print "Child process joined."
    a = parent_conn.recv()
    print "Received the following object."
    print "Type: %s. Size: %d." %(type(a), len(a))

Open in new window

from multiprocessing import Process, Queue

def f(q):
q.put('X' * 1000000)

if __name__ == '__main__':
    queue = Queue()
    p = Process(target=f, args=(queue,))
    p.join()                    # this deadlocks
    obj = queue.get()

Open in new window
beavoidSelf Employed


I'm so surprised by the deadlocking and complexity of this one!
I need multithreading to create an array of size n? What happened to b = new byte[n] ?
There is a bytearray object, but its size cant be specified?

Isn't there

array = bytearray[1024]

for p=0 to 1024
array[p]= random byte

A bytearray object (of Python) is a dynamic array, and as such it is similar to Python list that contains only integers from zero to 255. There is no new operator in Python. You create the bytearray object the same way as objects of other classes by simply using the class name followed by parentheses with possible arguments.

In this sense, Python is more similar to C++ (when creating static object or local objects) than to Java where all objects are created on the heap.

If you really need to initialize the bytearray object to the size of 1024 (say that after the inicialization you want to assign some bytes via indexing), you can create the object of the size by passing the argument that can be passed to the bytearray "constructor". It must be a sequence of what can be interpreted as bytes. Syntactically simplest way probably is to pass the bytes object of that size (i.e. immutable sequence of byte values). To do that, you can use the * operator. The single byte sequence if the bytes type with value of the byte equal to zero can be written as b'\x00'. When using the * operator after the bytes object, the new object of the bytes type is created by concatenation of  the first argument the given number times. This way, b'\x00' * 1024 creates a sequence of 1024 bytes.

When using the above sequence for initialization of the bytearray object, you get the wanted:
>>> ba = bytearray(b'\x00' * 1024)
>>> ba

Open in new window

Or you can create an empty bytearray object and append the bytes 1024 times, like this (with the same result):
>>> ba = bytearray()
>>> ba
>>> for i in range(1024):
...   ba.append(0)
>>> ba

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

beavoidSelf Employed



That looks promising, exactly what I need

What does the "bytearray(b'') " mean, line 3?

I will send these 1024 bytes over UDP from RTS server to each client, containing the game state.
Do you think 1024 is a practical size or 4096?

I may have units per player up to
- 20 home base buildings
- 15 workers
- 200 army, object units

making maybe 250+ units per player, meaning if giant game with 4 or 6 people, thousands of units, structures
Each unit has an x and y position, health, unit type,
= 4 or 5 integers per unit in the game state, 4 bytes per integer
5 * 250*4 = 5000 bytes.

4096 bytes for a complete game state message would not be enough without serious supply limits. I'd probably only be able to incorporate unit deltas to update the game state, with a complete game state message every 15 cycles?
Any ideas?

beavoidSelf Employed


Maybe I could send the full game state over 5,6 messages, or as many needed?
beavoidSelf Employed


pepr, thanks

If I do that appending many times to create a 1024 sized byte array, will I be able to send that object as the data portion uncorrupted? or must I split it up? Will the interpreter know how to handle it?

> What does the "bytearray(b'') " mean, line 3?

The example was copied from the interactive mode of Python (that is from the cmd console window when you just type python without arguments, and Python uses the >>> prompt and asks you for commands. When you type only the variable name, it displays the representation of the object.  You can also get the string representation of any object via calling the built-in function repr(ba). The representation (if possible) is a string that can be copy/pasted into a Python source code, and when executed, it creates the object with the same value.

The only way to get a bytearray object is to "call" bytearray(seq) where seq is any iterable object that returns small integer or bytes. There is a special syntax for expressing the objects of the bytes type. It looks as if you wrote a string literal, but the b is used as the prefix (before the opening quote character). The b'' is that b prefix, and the following two single quote characters wrap the empty content. You can also write bytearray() without any argument, but the repr() function does not uses that special case. It displays also the default value of the argument.

For the size of the message, 4 kB size is nothing if it is not sent too frequently. On the other hand, I have no experience with writing network games, and I cannot tell you what approaches are usual and efficient enough. Generally, you should always send as small quantity of data as possible -- the events only, with information that cannot be calculated or guessed at the other side (server). Using UDP, you should also think about the situation when the packet is lost or doubled. That kind of thinking must be the part of the design.

I suggest to pick sources of some real time strategy game (from pygame) and learn the things by reading their source code. But don't ask me what game you should choose. I do not know :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial