Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2014-03-03
Medium Priority
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 29

Accepted Solution

pepr earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 29

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

824 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