0

I'm building a very simple multiplayer web game. It's tron lightcycles, but to turn, the user must solve an arithmetic problem.

I've been working on a particular problem for 2-3 days because I didn't fully understand what was going on. Every time a game started, I would create an instance of a class called Flux in which to store game data. The problem was, since there are two players, two instances of Flux are created.

I need only one, and I need to be able to share this instance with the opposing player. How?

I'm using django 1.11.17, django-channels 1.x, python 3.6

I looked at this answer: Issue with setting value for Python class inherited from multiprocessing.Process

Is the multiprocessing module a good fit for my case?

1 Answer 1

0

no due to how channels runtime works there is no garenty multiprocessing will work.

what i suggest you do instread is:

  1. upgrade to channels v2
  2. create a db model that will hold your game state
  3. whenver a player does an action update the model and send a message over the channel group to the other player so that new state can be sent to the user.

for this last step you could make use of https://github.com/hishnash/djangochannelsrestframework this post gives some more deatils with respect to model deatils https://lostmoa.com/blog/DjangoChannelsRestFrameworkSubscribingToModels/

Sign up to request clarification or add additional context in comments.

6 Comments

I update game state every 100ms. That's too many database queries
what about writing that to redis instead of a db model?
I thought tried that. But I'd have to initialize a redis instance every time the game state changes. Initializing a redis instance takes anywhere from 2-6 seconds, which is way too long.
you should be able to keep a single instance active and have a single connection to it.
Where? The function, ws_receive, executes only when data is sent to it (through websocket). If I define it outside the ws_receive, ws_receive never sees it. If I put it inside the function, the instance is only defined for that window of time in which the ws_receive code is executed, for that one period.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.