Solved

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

Posted on 2014-03-03
7
6,785 Views
Last Modified: 2014-03-09
Hi
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?
Thanks
0
Comment
Question by:beavoid
  • 4
  • 2
7 Comments
 
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
    conn.send(a)
    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])
    proc.start()
    print "Child process started."
    print "Child process joined."
    a = parent_conn.recv()
    proc.join()
    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.start()
    p.join()                    # this deadlocks
    obj = queue.get()

Open in new window

http://stackoverflow.com/questions/15137292/large-objects-and-multiprocessing-pipes-and-send
0
 

Author Comment

by:beavoid
ID: 39905107
Thanks
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

?
Thanks
0
 
LVL 28

Accepted Solution

by:
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
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
[...snip...]
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00\x00\x00\x00\x00\x00')

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
bytearray(b'')
>>> for i in range(1024):
...   ba.append(0)
...
>>> ba

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:beavoid
ID: 39906539
thanks

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?

Thanks
0
 

Author Comment

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

Author Comment

by:beavoid
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?

Thanks
0
 
LVL 28

Expert Comment

by:pepr
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 :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now