[Haskell-beginners] Hints on how to remove unsafePerformIO from my function?

David McBride dmcbride at neondsl.com
Mon Jul 19 10:23:27 EDT 2010


Well, I thought about that, but I can't find anything in the network.socket
library that would allow me to see if there is any data ready to be read
from.

But besides that, wouldn't that cause a busy wait as the thread constantly
failed to read and yielded over and over again until there was a new
packet?  I'd rather if this thread chilled out until something worthwhile
occurs.

On Mon, Jul 19, 2010 at 10:12 AM, Benjamin Edwards
<edwards.benj at gmail.com>wrote:

> fork it into a thread and have it run on a loop, yielding if it can't read
> any data from the udp socket?
>
> On 19 July 2010 16:07, David McBride <dmcbride at neondsl.com> wrote:
>
>> I am writing a voip server to take advantage of haskells awesome threading
>> and parsing ability.  At first everything was going great, but my thread
>> that fetches udp makes use of unsafePerformIO:
>>
>> fetchUDPSIP :: TChan B.ByteString -> IO ()
>> fetchUDPSIP chan = do
>>   sock <- getUDPSocket 5060
>>   let results = (unstrict . repeat . getUDP) sock
>>   mapM_ (atomically . writeTChan chan) results
>>   where
>>     unstrict [] = []
>>     unstrict (x:xs) = unsafePerformIO x:unstrict xs
>>
>>
>> It fetches it from a socket, and then writes it to a TChan.  This results
>> in a stream of bytestrings that another thread can read from.  If I don't
>> use unsafePerformIO, then it tries to read all possible packets before
>> returning anything, and so it never writes to the TChan at all.  The problem
>> with doing it this way is that unsafePerformIO apparently stops every other
>> thread from doing anything while it is waiting for a packet.
>>
>> But I can't think of a way to rewrite this function to do what I want.
>> I'm kind of new to this, does anyone have any hints that could help me out?
>>
>> Here's a simple version without any of the implementation details:
>>
>> fetchLine = do
>>   let results = (unstrict . repeat) getLine
>>   mapM_ putStrLn results
>>   where
>>     unstrict [] = []
>>     unstrict (x:xs) = unsafePerformIO x:unstrict xs
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100719/a44f85e4/attachment-0001.html


More information about the Beginners mailing list