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

Posted on 2014-03-03
Last Modified: 2014-03-09
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?
Question by:beavoid
  • 4
  • 2
LVL 15

Expert Comment

by:Minh Võ Công
ID: 39902822
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

Author Comment

ID: 39905107
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

LVL 28

Accepted Solution

pepr earned 500 total points
ID: 39905697
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.


Author Comment

ID: 39906539

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?


Author Comment

ID: 39906623
Maybe I could send the full game state over 5,6 messages, or as many needed?

Author Comment

ID: 39907997
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?

LVL 28

Expert Comment

ID: 39908893
> 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 :)

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Read about why website design really matters in today's demanding market.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question