lundi 20 avril 2015

multiprocessing in IPython console on Windows machine - if __name_ requirement

I'm working with IPython and Spyder IDE on a Windows machine. When the IDE is starting, a set of py-files is loaded to define some functions that make my work a bit easier. Everything works as expected.

Now I would like to upgrade one of these function to use multiprocessing, but on Windows this requires the if __name__ == "__main__": statement. So it seems that I cannot call the function directly and pass the arguments from the IPython console.

For example one of the py-files (let's call it test.py) could look like the following code.

import multiprocessing as mp
import random
import string

# define a example function
def rand_string(length, output):
    """ Generates a random string of numbers, lower- and uppercase chars. """
    rand_str = ''.join(random.choice(
                string.ascii_lowercase
                + string.ascii_uppercase
                + string.digits)
           for i in range(length))
    output.put(rand_str)


def myFunction():
    # Define an output queue
    output = mp.Queue()        

    # Setup a list of processes that we want to run
    processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(4)]

    # Run processes
    for p in processes:
        p.start()

    # Exit the completed processes
    for p in processes:
        p.join()

    # Get process results from the output queue
    results = [output.get() for p in processes]

    print(results)

In my IPython console I would like to use the line

myFunction()

to trigger all the calculations. But on Windows a end up getting a BrokenPipe error.

When I put

if __name__ == "__main__":
     myFunction()

at the end of the py-file and run the complete file by

runfile(test.py)

it works. Of course. But that makes it very hard to pass arguments to the function as I always have to edit the test.py-file itself.

My question is: How do I get the multiprocessing function running without putting it in this if __name__ == "__main__": statement??

Ho do I get MySQL 5.6 to work with R/Rtools on a Windows 7 64 bit OS

OS - Windows 7 64 bit R v3.1.2 RTools v3.2.0.1948 MySQL 5.6

I have installed MySQL using the msi intaller for Windows and have checked that the software has been installed correctly - which it has.

Despite many hours and reading numerous posts and web pages I cannot get MySQL to work with R.

I have found the following directories: 1. c:/Program Files/MySQL 2. c:/Program Files (86)/MySQL 3. c:/ProgramData/MySQL and c:/ProgramData/MySQL/MySQL Server 5.6

I cannot find any folders called bin nor the files called libmysl.lib and libmysql.dll - I have checked for hidden files and directories.

There seems to be no posts which cover this issue properly so I would be grateful for as much help as possible.

How to properly measure & display owner drawn context menu item with a checkmark?

I'm simply trying to add small color swatches to my context menu (displayed via the TrackPopupMenu API.) Here's a Photoshopped version of what I'm trying to achieve:

enter image description here

As far as I understand the default menu does not support it. Btw, the sample above (without color swatches) was generated by doing this:

MENUITEMINFO mii = {0};
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STATE | MIIM_STRING;
mii.fType = MFT_STRING;
mii.wID = ID_1_MARKER_01 + m;
mii.dwTypeData = L"Marker";
mii.cch = TSIZEOF(L"Marker");
mii.fState = m == 1 ? MFS_CHECKED : MFS_ENABLED;
if(m == 2)
    mii.fState |= MFS_GRAYED;

VERIFY(::InsertMenuItem(hMenu, ID_1_BEFORE, FALSE, &mii));

So I discovered that I need to use MFT_OWNERDRAW style to draw menu items myself, but that's where the problems begin.

I changed my code to display my menu as such:

MENUITEMINFO mii = {0};
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STATE;
mii.fType = MFT_OWNERDRAW;
mii.wID = ID_1_MARKER_01 + m;
mii.dwItemData = MARKER_ID_01 + m;
mii.fState = m == 1 ? MFS_CHECKED : MFS_ENABLED;
if(m == 2)
    mii.fState |= MFS_GRAYED;

VERIFY(::InsertMenuItem(hMenu, ID_1_BEFORE, FALSE, &mii));

then I needed to override WM_MEASUREITEM and WM_DRAWITEM messages. But when I do it with the code that I'll show below, here's what I get:

enter image description here

So please bear with me. I have several questions on this topic:

1) While processing WM_MEASUREITEM how am I supposed to know the size of text if they do not supply neither DC nor HWND for the menu? In other words, if I do this, the size of the menus is wrong:

#define TSIZEOF(f) ((sizeof(f) - sizeof(TCHAR)) / sizeof(TCHAR))

//hwnd = HWND supplied in WM_MEASUREITEM notification
HDC hDC = ::GetDC(hwnd);
HGDIOBJ hOldFont = ::SelectObject(hDC, ::SendMessage(hwnd, WM_GETFONT, 0, 0));

SIZE szTxt = {0};
::GetTextExtentPoint32(hDC, 
    L"Marker", 
    TSIZEOF(L"Marker"), 
    &szTxt);

//lpmis = MEASUREITEMSTRUCT*
lpmis->itemWidth = szTxt.cx;
lpmis->itemHeight = szTxt.cy;

::SelectObject(hDC, hOldFont);
::ReleaseDC(hwnd, hDC);

2) Then while processing WM_DRAWITEM how do I know the offset to begin drawing text on the left? If I do this, my menus aren't offset enough to the right (as you can see from the screenshot above):

int nCheckW = ::GetSystemMetrics(SM_CXMENUCHECK);

//lpdis = DRAWITEMSTRUCT*
::ExtTextOut(lpdis->hDC, 
    lpdis->rcItem.left + nCheckW, 
    lpdis->rcItem.top, 
    ETO_OPAQUE, 
            &lpdis->rcItem, 
    L"Marker", 
    TSIZEOF(L"Marker"), 
    NULL);

3) And lastly how do I draw that default checkbox on the left of the menu item?

Installing Debian on a machine without Ethernet, USB Drive or a CD-ROM

A Friend of mine asked me to format and install Debian on is laptop. It is Compaq Evo n600c. When there was still Windows XP installed on the machine, I downloaded the Debian Installer (lazy) EXE and ran it. Afterwards, in the setup process, in the Base System Installation there were some problems. It suggested to run this step again, so I did that.

When I boot up the computer, it reads:

Waiting for root file system ... done.
Gave up waiting for root device. Common problems:
- Boot args
  - Check rootdelay= (did the system wait long enough?)
  - Check root= (ded the system wait for the right device?)
- Missing modules (car /proc/modules; ls dev)
ALERT! /dev/block/8:33 does not exist. Dropping to a shell!
modprobe: module ehci-hcd not found in modules.dep.
modprobe: module uhci-hcd not found in modules.dep
modprobe: module ohci-hcd not found in modules.dep

BusyBox v1.20.2 (Debian 1:14.20.0-7) built in shell (ash)

/proc/cmdline

auto BOOT_IMAGE=Linux ro root=821

/proc/modules

sd_mod 25425 0 - Live 0xe081a000
crc_t20dif 12332 1 sd_mod, Live 0xe07f6000
ata_generik 12436 0 - Live 0xe07e7000

...and more, such as thermal, thermal system etc.

I tried to check with all of the common problems suggested, but as the message reads, the problems is that some modules are missing (checked the folders, not there). BUT! The real problem instead of just "formatting it and there you go" is that boot through Ethernet won't work (I finally got the server running, but Boot Agent is blind and won't see it). Also, there is no such option as "boot from USB flash", there are only "Boot from MultiBay", "Boot from Hard Drive" and "Boot over Ethernet". The hard drive does not have any other OS on it, so switching to it and re-installing is not an option.

I seek and try to think of a way to maybe skip without these modules and then update and upgrade the system so it runs normally. The system specs are LILO 27 boot loader (except the primitive (sorry) Compaq boot loader, that can't read USB Flash Drives, thumb drives or whatever.) Debian and BusyBox version can be seen in the first console log.

I played around with the initramfs.conf and the overriding config file, the output was not different.

Gwt plugin doesn't work in Chrome 42

The new version of chrome (42) doesn't support gwt plugin on windows 8.1 even I change the compatibility mode to Windows 7 I still get prompted to download the plugin again. I've tried removing it and re-installing but I still get this message. Any ideas?

Converting Windows 8 byte DateTime in Java

I have been looking very closely at the solution given in Foxpro, convert 8 byte datetime in java Date object

But, perhaps, owing to my inability in understanding the format, I have been unable to make it work in cases where I am trying to convert dates maintained in the Windows registry in 8 byte data format.

Trying to convert something like this 70 f9 48 b3 cc 78 d0 01, results in the following Tue Jan 27 13:57:19 IST 3528500; which is not quite the expected output.

Please help.

Deleting environment variables doesn't work

Why deleting an environment variable with reg delete HKCU\Environment /F /V TestVar in Windows 7 Professional removes it from the registry, but the variable still exists?

Here are the details: I created the following 3 .cmd files:

Check variable.cmd

echo TestVar = %TestVar%
pause

Set variable.cmd

setx TestVar 123
pause

Delete variable.cmd

reg delete HKCU\Environment /F /V TestVar
pause

Then I follow these steps (double clicking to make sure that I start a new session every time):

  1. Double click on Check variable.cmd and I see that TestVar does not exist
  2. Double click on Set variable.cmd and it says SUCCESS: Specified value was saved.
  3. Double click on Check variable.cmd and it shows the variable value. Good!
  4. Double click on Delete variable.cmd and it says The operation completed successfully.
  5. Double click on Check variable.cmd and it still shows the variable value. Bad!
  6. Click on the Start menu, type environment, click on Edit environment variables for your account to open the Environment Variables dialog box, click OK without changing anything
  7. Double click on Check variable.cmd and the variable does not exist anymore

I can find the value in the registry after step 2, I cannot find it after step 4, but step 5 still finds it. And even if I don't change anything, step 6 really deletes the variable.