introduction and first question about multithreading

Dave Angel d at davea.name
Thu Jul 12 09:52:35 EDT 2012


On 07/11/2012 03:51 AM, Vojtěch Polášek wrote:
> Greetings,
> My name is Vojta and I am blind student. I am slowly learning Python for
> about 4 years and I like it alot, mostly its ability to run on various
> platforms.
> My primary system is Ubuntu 12.04, but I have Windows XP at hand. I am
> using python 2.7. I have learned basics from the book A byte of Python
> (for version 2.X) and something from Dive into Python. But most things I
> learned by trial and error and thanks to solutions on stackoverflow.com.
> I Don't know much about good programming concepts in Python, just in
> general, so feel free to educate me about them.
>
> I haven't created anything great yet, but I am working on a game for
> blind people. It is a simple game of reaction, but I have to start with
> something, my next plan is Sudoku game.
> I am using Pygame for handling of sounds and keyboard events, speech
> dispatcher under Linux and pyttsx under Windows to provide speech
> output, numpy for sound cutting and my two little and simple modules for
> providing menu system and unified interface for both speech engines.
> During the development, I discovered, that I have to use multithreading
> to be able to check for key presses and manage the game at the same
> time. I tried threading module but this wasn't enough for me, because
> function running in the separate thread needed to access variables in
> the main thread at least I couldn't find the way how to do that. So I
> switched to multiprocessing and started using pipes for communication
> between processes. All is working well, except for one thing.
> My menu module uses a loop to check for keyboard events through pygame.
> I don't import pygame into my module directly, but rather pass it
> through my main module like this:
> menu.pygame = pygame
> All was working well until I started using multiprocessing. I may have a
> probable cause but I need your help.
> In my main module I am running a similar loop for checking keyboard
> input in separate process. When a player loses, the loop finishes and I
> join the process. Then the menu module kicks in and should launch its
> own loop checking for pygame keyboard events, but right after doing it
> it prints:
> [xcb] Unknown sequence number while processing queue
> [xcb] Most likely this is a multi-threaded client and XInitThreads has
> not been called
> [xcb] Aborting, sorry about that.
> python: ../../src/xcb_io.c:273: poll_for_event: Assertion
> `!xcb_xlib_threads_sequence_lost' failed
> received SigAbrt - core dumped
> Are atachments allowed here? I can send you my whole code.
> Please help me, if you can.
> Thank you very much,
> Vojta

Welcome to python-list.  I'm glad you're enjoying Python.

I haven't used pygame, but I know that (like almost all GUI toolkits) it
uses an event loop for interacting with at least the keyboard.  You
should be able to add other code to that event loop in some manner,
without going to the trouble of either multiple threads or multiple
processes.

But I'll ignore that and talk about multithreading.  You say

>function running in the separate thread needed to access variables in
>the main thread at least I couldn't find the way how to do that

but there is nothing special about threads.  If you can figure out what
namespace a variable is in, you can access it.  In fact, one large
reason people switch from multithread to multiple processes is to avoid
the accidental reuse of variables.

As for posting large code, you can upload it to a site like  pastebin,
and put a link in your message here.  Don't expect too much, however, as
most of us are averse at looking at a large fragment of somebody else's
code.





-- 

DaveA




More information about the Python-list mailing list